开发者

How do I convert a series of similar lines into a for loop?

开发者 https://www.devze.com 2022-12-25 00:42 出处:网络
I\'m programming an app for the iPhone.I\'m not very good with loops just yet.How do I shorten this code intoa for loop?

I'm programming an app for the iPhone. I'm not very good with loops just yet. How do I shorten this code into a for loop?

if(CGRectContainsRect([space1 frame], [box frame])){

  space1.image = [UIImage imageNamed:@"box.png"]开发者_运维问答;
 }
 else if(CGRectContainsRect([space2 frame], [box frame])){

  space2.image = [UIImage imageNamed:@"box.png"];
 }
 else if(CGRectContainsRect([space3 frame], [box frame])){

  space3.image = [UIImage imageNamed:@"box.png"];
 }
 else if(CGRectContainsRect([space4 frame], [box frame])){

  space4.image = [UIImage imageNamed:@"box.png"];
 }
 else if(CGRectContainsRect([space5 frame], [box frame])){

  space5.image = [UIImage imageNamed:@"box.png"];
 }


NSArray * spaces = [NSArray arrayWithObjects:space1, space2, space3, space4, space5, nil];
for (Space * space in spaces) {
  if (CGRectContainsRect([space frame], [box frame])) {
    space.image = [UIImage imageNamed:@"box.png"];
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消