【发布时间】:2011-08-17 23:53:33
【问题描述】:
运行代码时出现错误。罪魁祸首是我从下面的 plist 中访问了一个字符串:
NSString *sImageFile = [dictionary objectForKey:@"answerCorrect"];
NSLog(@"%@",sImageFile);
我的 cocos2d Init 中有这个:
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
NSUserDefaults *sud = [NSUserDefaults standardUserDefaults];
NSString *ctlLang = [sud valueForKey:@"ctlLang"];
NSNumber *questionState = [sud valueForKey:@"questionState"];
NSNumber *scoreState = [sud valueForKey:@"scoreState"];
gameArray = (NSMutableArray *)[sud valueForKey:@"gameArray"];
for (NSString *element in gameArray) {
NSLog(@"\nQL gameArray value=%d\n", [element intValue]);
}
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:ctlLang];
dictionary = [NSDictionary dictionaryWithContentsOfFile:finalPath];
NSString *sImageFile = [dictionary objectForKey:@"answerCorrect"];
NSLog(@"%@",sImageFile);
}
}
字符串的打印在场景的 init 部分可以正常工作。问题出现在我稍后定义的方法中。由于某种原因,它没有在此处显示的方法中返回字符串:
-(void) checkAnswer: (id) sender {
CGSize size = [[CCDirector sharedDirector] winSize];
CCMenuItemSprite *sAnswer = (CCMenuItemSprite *)sender;
NSLog(@"Checking Answer Tag is ---> %d",sAnswer.tag);
NSString *sImageFile = [dictionary objectForKey:@"answerCorrect"];
NSLog(@"%@",sImageFile);
if ([question.answer integerValue] == sAnswer.tag) {
//...
}
}
我在这里缺少什么?程序在 NSLog 语句处炸弹。
【问题讨论】:
-
通过valueForKey,你的意思是objectForKey吗?无论如何,也许你没有保留你需要的东西?
-
是的,它的 objectForKey 与代码中一样。这是我在界面中定义它的方式:@property (nonatomic, retain, readonly) NSDictionary *dictionary;
标签: iphone objective-c ios nsstring nsdictionary