1

我正在使用 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];

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

但是,当我稍后尝试阅读时,它崩溃了。这是我读取文件的方式:(数据参数来自保管箱文件。保管箱 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;
}

4

1 回答 1

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

于 2013-05-22T01:29:39.810 回答