2013-03-01 59 views
1

有我的方式把[objects objectNamed:@"level"[insert i]"start"];变量或对象名

这是我能做到这一点,现在唯一的办法,但我想我与替换它的数字。

CCTMXObjectGroup *objects = [theMap objectGroupNamed:@"level1_oj"]; 
NSMutableDictionary *start1; 
if(i ==0) 
{ 
    start1 = [objects objectNamed:@"level1start"]; 
} 
if(i ==1) 
{ 
    start1 = [objects objectNamed:@"level2start"]; 
} 

CGFloat x = [[start1 valueForKey:@"x"]intValue]; 
CGFloat y = [[start1 valueForKey:@"y"]intValue]; 

levelstart[i]=ccp(((x/2)-i*480),y/2); 

回答

5

用途:

[objects objectNamed:[NSString stringWithFormat:@"level%dstart", i]]; 

您需要使用stringWithFormat:对于这种情况。可以使用%d%i来表示整数,并且可以使用%@来插入字符串。这里是documentation

+0

非常感谢你! – user2121776 2013-03-01 01:07:39

+0

完成,对不起有点新的这个很酷的网站 – user2121776 2013-03-01 01:14:32

+0

谢谢@ user2121776。不是问题。 :) – iDev 2013-03-01 01:15:09

2

看一看在NSString Documentation

你想用[NSString stringWithFormat:]

有关格式的更多细节看看Formatting String Objects

+0

+1,既然这两个答案都是同时发布的,你的答案也是正确的。 – iDev 2013-03-01 07:06:13

+0

Thx!有时候我更愿意提供一点信息来鼓励人们阅读文档。 – Sebastian 2013-03-01 10:50:00