Objective c 对象有效时NSDictionary writeToFile失败,权限为0k

Objective c 对象有效时NSDictionary writeToFile失败,权限为0k,objective-c,macos,file,nsdictionary,Objective C,Macos,File,Nsdictionary,为什么NSDictionary无法编写??我已经检查了字典的内容:所有实例都是NSString和NSNumber。我检查了权限:在同一路径上具有相同名称的文本文件写得很好。当然,我的字典不是空的 NSString *file = ... NSDictionary *dict = ... // check dictionary keys BOOL wrong = NO; for (id num in [dict allKeys]) { if (![num isKindOfClass:[N

为什么
NSDictionary
无法编写??我已经检查了字典的内容:所有实例都是
NSString
NSNumber
。我检查了权限:在同一路径上具有相同名称的文本文件写得很好。当然,我的字典不是空的

NSString *file = ...
NSDictionary *dict = ...

// check dictionary keys
BOOL wrong = NO;
for (id num in [dict allKeys]) {
    if (![num isKindOfClass:[NSNumber class]]) {
        wrong = YES;
        break;
    }
}
if (wrong) {
    NSLog(@"First");
}
// check dictionary values
wrong = NO;
for (id num in [dict allValues]) {
    if (![num isKindOfClass:[NSString class]]) {
        wrong = YES;
        break;
    }
}
if (wrong) {
    NSLog(@"Second");
}

if (![dict writeToFile:file atomically:YES]) {
    // 0k, let's try to create a text file
    NSLog(@"Names writing error!");
    [@"Something here... .. ." writeToFile:file atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
输出:“名称写入错误!”

已成功创建文本文件。

写出字典将创建属性列表,并且根据属性列表中的所有键必须是字符串

。。。尽管NSDictionary和CFDictionary对象允许它们的键 可以是任何类型的对象,如果键不是字符串对象,则 集合不是属性列表对象


NSNumber
不支持将对象作为键。

正如@vadian所指出的,不能使用数字键写入plist。但是您可以使用
NSKeyedArchiver

NSURL *documents = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:false error:nil];
NSURL *fileURL = [documents URLByAppendingPathComponent:@"test.plist"];

// this will not work

NSDictionary *dictionary = @{@1: @"foo", @2: @"bar"};
BOOL success = [dictionary writeToFile:fileURL.path atomically:true];
NSLog(@"plist %@", success ? @"success" : @"failure");

// this will

fileURL = [documents URLByAppendingPathComponent:@"test.bplist"];
success = [NSKeyedArchiver archiveRootObject:dictionary toFile:fileURL.path];
NSLog(@"archive %@", success ? @"success" : @"failure");
// to read it back

NSDictionary *dictionary2 = [NSKeyedUnarchiver unarchiveObjectWithFile:fileURL.path];
NSLog(@"dictionary2 = %@", dictionary2);
您可以使用
NSKeyedUnarchiver
将其读回:

NSURL *documents = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:false error:nil];
NSURL *fileURL = [documents URLByAppendingPathComponent:@"test.plist"];

// this will not work

NSDictionary *dictionary = @{@1: @"foo", @2: @"bar"};
BOOL success = [dictionary writeToFile:fileURL.path atomically:true];
NSLog(@"plist %@", success ? @"success" : @"failure");

// this will

fileURL = [documents URLByAppendingPathComponent:@"test.bplist"];
success = [NSKeyedArchiver archiveRootObject:dictionary toFile:fileURL.path];
NSLog(@"archive %@", success ? @"success" : @"failure");
// to read it back

NSDictionary *dictionary2 = [NSKeyedUnarchiver unarchiveObjectWithFile:fileURL.path];
NSLog(@"dictionary2 = %@", dictionary2);
注意,您可以使用任何符合(并正确实现)
NSCoding
的类来执行此操作。幸运的是,
NSDictionary
已经符合。您必须确保字典中的任何对象也符合要求(包括
NSString
NSNumber
do)。如果你的字典里有一个自定义对象,你必须使它与你自己的字典完全一致


这些都在。

中描述过,您在哪里写?打包的?如果你不能这样做,至少在iOS上,你必须在一个新的文件路径上写入。@Larme在桌面上的一个文件夹Mac OS中。