This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable,
visit the help center。
7年前关闭。
我正在使用NSPropertyListSerialization通过Dropbox Sync API同步NSDictionary。写入文件可以正常工作,它会显示在Dropbox中,但尝试读取该文件会导致EXC_BAD_ACCESS崩溃。
这是我写到保管箱的方法:
//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];
这可以正常工作,并且永远不会记录该错误。
但是,当我稍后尝试阅读时,它崩溃了。
这是我读取文件的方式:(data参数来自dropbox文件。dropboxSDK在获取此文件时也不会给出错误。)
+ (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;
}
我知道了:
+ (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