writeToPath:atomically: - 不确定为什么这会失败

时间:2012-03-24 18:13:38

标签: iphone objective-c nsdictionary nsdata

这是我的代码。 NSDictionary曾经是某种东西,但我试图找到问题时暂时删除了它。

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Entries/"];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDirectory = NO;
    BOOL directoryExists = [fileManager fileExistsAtPath:path isDirectory:&isDirectory];
    if (!directoryExists) {
        [fileManager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];
    }

    if ([self.pathName isEqualToString:@""] || self.pathName == nil) {
    self.pathName = [NSString stringWithFormat:@"%@.JEntry", [JMedia generateUuidString]];
    }

    NSString  *entryPath = [path stringByAppendingPathComponent:self.pathName];

    NSDictionary *dictionary;

    NSLog(@"entrypath: %@", entryPath);

    BOOL success = [dictionary writeToFile:entryPath atomically:YES];

    NSLog(@"success: %i", success);

的NSLog:

entrypath: /var/mobile/Applications/1B838285-8326-427A-8AC5-0D5567C3CD81/Documents/Entries/70AFCF6D-540E-436E-9989-68793500E35B.JEntry
success: 0

1 个答案:

答案 0 :(得分:2)

问题可能是dictionary包含的项目不是documentaion所说的属性列表对象:

  

此方法在写出文件之前以递归方式验证所有包含的对象是属性列表对象(NSData,NSDate,NSNumber,NSString,NSArray或NSDictionary的实例),如果所有对象都不是,则返回NO属性列表对象,因为生成的文件不是有效的属性列表。