2012-03-24 73 views
-2

這是我有的代碼。 NSDictionary確實曾經是一些東西,但是我試圖找到問題時暫時刪除了它。writeToPath:原子地: - 不知道爲什麼這是失敗

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 
+0

你檢查了SIM的文檔目錄,看看文件是否存在? – CodaFi 2012-03-24 18:23:48

+0

'[JMedia生成UuidString]'方法會帶回一個完全隨機的字符串,因此它不可能已經存在。 – Andrew 2012-03-24 18:28:50

+1

目前'字典'是零,所以這是永遠不會工作。 – jrturton 2012-03-24 18:31:10

回答

2

的問題可能是dictionary包含不是屬性列表對象的項目,如documentaion說:

這種方法遞歸地驗證了所有包含的對象在寫出文件之前是屬性列表對象(NSData,NSDate,NSNumber,NSString,NSArray或NSDictionary的實例),並且返回NO對象不是屬性列表對象,因爲生成的文件不是有效的屬性列表。

+0

我想到了這一點,這就是爲什麼我讓NSDictionary不包含任何對象。所以我知道這不是問題。 – Andrew 2012-03-24 18:28:10

+2

否。在你當前的代碼中,'NSDictionary'不是一個不包含任何對象的字典,它是'nil'。這就是爲什麼你現在獲得成功:0。 – sch 2012-03-24 18:30:14

相關問題