【问题标题】:Why can't I serialize this data?为什么我不能序列化这些数据?
【发布时间】:2011-02-18 00:51:17
【问题描述】:

我想序列化一个数组和一个整数。当我只保存整数时,它可以工作。当我添加数组时,它失败了。

    NSArray *tempArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsPath = [tempArray objectAtIndex:0];

    tempArray = nil;
    tempArray = [NSArray arrayWithObjects:
                 ( (someOtherArray != nil) ? (id) someOtherArray : [NSNull null] ),
                 [NSNumber numberWithInt:someIntValue],
                 nil];

    NSString *filePath = [NSString stringWithFormat:@"%@/%@.plist", docsPath, kDataFilename];
    NSString *errString;
    NSData *serializedStrings= [NSPropertyListSerialization
                                dataFromPropertyList:tempArray
                                format:NSPropertyListBinaryFormat_v1_0
                                errorDescription:&errString];
    if( errString != nil ) {
        NSLog(@"SaveData NSpropertyListSerialization error: %@", [errString description]);
        [errString release];
    }

    // returns NO
    BOOL success = [serializedStrings writeToFile:filePath atomically:YES];

数组可能为零,所以我先检查一下。我还尝试了 [NSNumber numberWithInt:0] 而不是 [NSNull null] 只是想看看它是否可以代替 null 工作,但它没有。

我还得到:NSpropertyListSerialization 错误:属性列表对格式无效

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    您不能将+[NSNull null] 存储在 plist 中。您需要像占位符值这样的解决方法(即使 NSNull 本身就是一个占位符值......)

    【讨论】:

    • 那么为什么: ( (someOtherArray != nil) ? (id) someOtherArray : [NSNumber numberWithInt:0] ) 失败了?
    • 原来有时数据是 nil,并给了我 NSNull 的错误,当我测试 numberWithInt 时,数据在数组中,但是它的数据本身存在序列化错误大批。谢谢!
    猜你喜欢
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多