【发布时间】:2012-08-20 07:05:08
【问题描述】:
抱歉,帖子太长了。 当我第二次使用它时,来自 plist 的字符串会使我的程序崩溃。 我有一个场景和一个对象的 cocos2d 项目 (attach)。还有一个 .plist 文件
HelloWorldLayer.h & .m (instantiate from CCLayer)
NodeObject.h & .m (instantiate from CCNode)
Test.plist
在 NodeObject 我有一个本地字符串和两个方法
@interface NodeObject : CCNode
{
NSString *stringForPrint;
}
-(void)createObjWithString:(NSString *)string;
-(void)printString;
在这两种方法中,我们打印在参数string
中获得的字符串-(void)createObjWithString:(NSString *)string
{
stringForPrint = string;
NSLog(@"NodeObject.createObjWithString stringForPrint >> %@", stringForPrint);
}
-(void)printString
{
NSLog(@"NodeObject.printString stringForPrint >> %@", stringForPrint);
}
Plis 内容是一个数组,其中包含一个带有项目类型字符串的字典。
Root
-TestArray <Array>
--item 0 <Dictionary>
---type <String> == "This is string from plist"
为了测试,我在场景中创建 NodeObject 并从 plist 中获取数据。并打印这个字符串。它工作正常。
if ([testDictionary objectForKey:@"TestArray"]) {
for (NSDictionary *tempItemData in [testDictionary objectForKey:@"TestArray"]) {
NSLog(@"type form plist in for loop > %@", [tempItemData objectForKey:@"type"]);//It's cool work. True string from plist.
}
}
我将我的 NodeObject 创建到循环中。它又开始工作了。
if ([testDictionary objectForKey:@"TestArray"]) {
for (NSDictionary *tempItemData in [testDictionary objectForKey:@"TestArray"]) {
NodeObject *testObj = [NodeObject node];
//and send it string from plist
[testObj createObjWithString:[tempItemData objectForKey:@"type"]];
}
}
但是!如果我尝试在 printString 方法表单 NodeObject 中使用此字符串,应用程序将在没有日志的情况下崩溃。 [testObj 打印字符串]; //崩溃应用程序
我重复一遍。使用手动字符串工作创建对象。如果使用 plist 中的字符串,则会崩溃。 我摔断了头。并且仅在第二种方法中。在 createObjWithSrig 中它可以工作。
//Work
NodeObject *testObj = [NodeObject node];
[testObj createObjWithString:@"manual string object"];
[testObj printString]; // Print correct string
//Crash
NodeObject *testObj = [NodeObject node];
[testObj createObjWithString:[tempItemData objectForKey:@"type"]];
[testObj printString]; //Crash here
我附上project file。可以测试一下
【问题讨论】:
标签: ios nsstring cocos2d-iphone plist