如何在NSPropertyListSerialization中修复此EXC_BAD_ACCESS?

时间:2013-05-22 01:05:22

标签: ios objective-c dropbox-api

我正在使用NSPropertyListSerialization通过Dropbox Sync API同步NSDictionary。写入文件工作正常,它显示在Dropbox中,但尝试阅读它与EXC_BAD_ACCESS崩溃。

以下是我写信给dropbox的方法:

//Create the dictionary, add the necessary stuff then do this.
NSData *data = [NSPropertyListSerialization dataWithPropertyList:syncPlistDictionary
                                                              format:NSPropertyListXMLFormat_v1_0
                                                             options:0
                                                               error:NULL];
    DBError *error = nil;
    [syncFile writeData:data error:&error];
    if (error) {
        NSLog(@"Dropbox Error writing to file: %@", error);
    }
    [syncFile close];

这样可以正常工作,它永远不会记录该错误。

然而,当我稍后尝试阅读时,它会崩溃。 以下是我正在读取文件的方法:(数据参数来自dropbox文件。收件箱SDK在获取此文件时也不会出错。)

+ (NSDictionary *)dictionaryWithContentsOfData:(NSData *)data
{
    NSData *myData = [data copy];
    if ([myData length]==0) {
        return [[[NSDictionary alloc] init] autorelease];
    }
    if (!myData) {
        return nil;
    }
    // uses toll-free bridging for data into CFDataRef and CFPropertyList into NSDictionary
    NSError *error = nil;
    NSDictionary *dictionary = [NSPropertyListSerialization propertyListWithData:myData
                                                                         options:0
                                                                          format:NSPropertyListXMLFormat_v1_0
                                                                           error:&error];
    if (error) {
        NSLog(@"NSDictionary Helper Error: %@", error); //This never gets logged because it crashes before getting here.
    }
    //[myData release]; //I commented this out thinking I was probably releasing it to fast but it made no difference.
    return dictionary;
}

1 个答案:

答案 0 :(得分:3)

我明白了:

+ (NSDictionary *)dictionaryWithContentsOfData:(NSData *)data
{
    NSData *myData = [data copy];
    if ([myData length]==0) {
        return [[[NSDictionary alloc] init] autorelease];
    }
    if (!myData) {
        return nil;
    }
    // uses toll-free bridging for data into CFDataRef and CFPropertyList into NSDictionary
    NSError *error = nil;
    NSPropertyListFormat plistFormat;
    NSDictionary *dictionary = [NSPropertyListSerialization propertyListWithData:myData
                                                                         options:0
                                                                          format:&plistFormat
                                                                           error:&error];
    if (error) {
        NSLog(@"NSDictionary Helper Error: %@", error);
    }
    [myData release];
    return dictionary;
}

我所要做的就是:NSPropertyListFormat plistFormat;format:&plistFormat而不是format:NSPropertyListXMLFormat_v1_0