【问题标题】:String from plist not standard string? Crash app using string from plist. iOSplist中的字符串不是标准字符串?使用 plist 中的字符串使应用程序崩溃。 iOS
【发布时间】: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


    【解决方案1】:

    您可以使用descriptionclass 方法检查变量背后隐藏的内容。

    所以试试

    NSLog(@"Description %@ and class %@",[[tempItemData objectForKey:@"type"]description],[[tempItemData objectForKey:@"type"]class]);

    这可能会给你一些关于哪里出了问题的信息——也许它是空的)

    【讨论】:

    • 您好,感谢您的提示。该日志说对象类是__NSCFString。 Avery wear 我读过关于将 __NSCFString 转换为 NSString 的文章,没有理由这样做。但我对 __NSCFString 有问题,对 NSStribg 没有。你怎么看?
    【解决方案2】:

    在 Apple 论坛上,我找到了答案。 我从 plist 中取出的对象是自动释放的。它不能只是分配和以后使用。

    我应该使用保留或复制。

    printString = [string retain];
    

    解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2012-05-22
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多