【问题标题】:writeToPath:atomically: - not sure why this is failingwriteToPath:atomically: - 不知道为什么会失败
【发布时间】:2012-03-24 18:13:38
【问题描述】:

这是我的代码。 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

【问题讨论】:

  • 你检查过sim的文档目录看文件是否存在?
  • [JMedia generate UuidString] 方法返回一个完全随机的字符串,因此它不可能已经存在。
  • 目前dictionary 为零,所以这永远行不通。
  • 在去掉dictionary的值之前能不能贴一下原代码?

标签: iphone objective-c nsdictionary nsdata


【解决方案1】:

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

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

【讨论】:

  • 我想到了,这就是为什么我让 NSDictionary 不包含任何对象。所以我知道这不是问题。
  • 没有。在您当前的代码中,NSDictionary 不是不包含任何对象的字典,而是nil。这就是您现在收到success: 0 的原因。
猜你喜欢
  • 2017-04-24
  • 2014-04-08
  • 2020-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-25
相关资源
最近更新 更多