Ios plist中的字符串不是标准字符串?使用plist中的字符串崩溃应用程序。网间网操作系统
抱歉发了这么长的邮件。 当我第二次使用plist中的字符串时,它会使我的程序崩溃。 我有一个cocos2d项目()和一个场景和一个对象。和一个.plist文件Ios plist中的字符串不是标准字符串?使用plist中的字符串崩溃应用程序。网间网操作系统,ios,nsstring,cocos2d-iphone,plist,Ios,Nsstring,Cocos2d Iphone,Plist,抱歉发了这么长的邮件。 当我第二次使用plist中的字符串时,它会使我的程序崩溃。 我有一个cocos2d项目()和一个场景和一个对象。和一个.plist文件 HelloWorldLayer.h & .m (instantiate from CCLayer) NodeObject.h & .m (instantiate from CCNode) Test.plist 在节点对象中我有一个本地字符串和两个方法 @interface NodeObject : CCNode {
HelloWorldLayer.h & .m (instantiate from CCLayer)
NodeObject.h & .m (instantiate from CCNode)
Test.plist
在节点对象中我有一个本地字符串和两个方法
@interface NodeObject : CCNode
{
NSString *stringForPrint;
}
-(void)createObjWithString:(NSString *)string;
-(void)printString;
在这两种方法中,我们都打印参数字符串中获得的字符串
Plis内容是一个数组,其中包含一个带有项类型字符串的字典
Root
-TestArray <Array>
--item 0 <Dictionary>
---type <String> == "This is string from plist"
我将我的节点对象创建到循环中。又是工作
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中的字符串,则会导致崩溃。
我摔断了头。只有在第二种方法中。在CreateObjWithTrig中,它可以工作
//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
我很高兴。可以对此进行测试您可以使用
description
和class
方法检查变量中的内容
所以试试看
NSLog(@“Description%@和class%@),[[tempItemData objectForKey:@“type”]Description],[tempItemData objectForKey:@“type”]class])代码>
这可能会给你一些关于哪里出了问题的信息——可能是空的)在苹果论坛上,我找到了答案。
我从plist中取出的对象是自动释放的。它不能只是分配并在以后使用
我应该使用保留或复制
printString = [string retain];
已解决此问题。您好,谢谢您的提示。此日志表示对象类为uu NSCFString。Avery wear我读过关于将u NSCFString转换为NSString的文章,没有理由这么做。但我对NSCFString有问题,而对NSStribg没有问题。你怎么认为?
//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
printString = [string retain];