0
我試圖將數據寫入到我plist
文件,這是在Library/Caches
在我的應用程序的沙箱創建,具有下列定製的輔助方法:未能我的自定義詞典中的數據寫入到plist文件
-(void)saveStatusesToPlist:(NSArray *)array
{
NSDictionary *dict = @{@"data": array};
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
NSString *plistPath = [cachesDirectory stringByAppendingPathComponent:filepath];
NSLog(@"PATH: %@", plistPath);
NSFileManager *manager = [NSFileManager defaultManager];
if (![manager fileExistsAtPath:plistPath]) {
BOOL isCreated = [manager createFileAtPath:plistPath contents:nil attributes:nil];
NSLog(@"Result:%@", isCreated? @"Success": @"Failed");
}
BOOL flag = [dict writeToFile:plistPath atomically:YES];
NSLog(@"Write result:%@", flag? @"Success": @"Failed");
}
請注意參數:array
是從JSON
轉換而來的多個NSDictionary
對象的數組,這些數據是通過網絡從服務器端獲取的。
UPDATE: 我得到了我的數組是這樣的:
NSArray *array = [result objectForKey:@"array"];
但結果卻總是失敗。
所以我懷疑這個問題來自array
,它可能包含無效數據。然後我試着硬編碼array
,好像@[@"key1": @"value1", @"key2": @"value2", @"key3": @"value3"]
。
對於硬編碼數據,flag
返回YES
表示已保存。
所以,至少我可以告訴我提取的數據有一些問題,但我不知道它在哪裏。
如果需要,我可以在這裏粘貼我的字典模型實例 – Boris
它在我的項目中非常完美...我試圖寫我自己的json數組... 您可以粘貼您的模型之一嗎? – Che
@Che我的一個字典是相當大,其中包含很多對象,所以我在這裏粘貼屏幕截圖... – Boris