[英]Why don't I get an Array from a NSKeyedUnarchiver?
這是我的代碼:
NSString * path = kCachePath(kCacheFileName);
NSMutableArray * arry = [[NSMutableArray alloc]init];
arry = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
// NSData * data = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
// NSError *error = nil;
// arry = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &error];
if (arry) {
[arry addObject:playModel];
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:arry];
[NSKeyedArchiver archiveRootObject:data toFile:path];
return YES;
} else {
NSMutableArray * ary = [NSMutableArray array];
[ary addObject:playModel];
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:ary];
[NSKeyedArchiver archiveRootObject:data toFile:path];
}
我已經成功地將數組歸檔到文件中,但是當我想解包數據文件時,得到了NSMutableData', But I want an
NSMutableArray . and I have tried the
. and I have tried the
NSJSONSerialization to conver the
NSdata轉換to
NSMutableArray and I got a
為零。 有人知道哪里出問題了嗎?
這是我的cachePath:
#define kCacheFileName @"fakeLiveHistory.data"
#define kCachePath(kCacheFileName) ([NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:kCacheFileName])
您正在歸檔數據-不是數組,因此下次您很顯然會返回數據;)
存檔數組作為開始
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:arry];
[NSKeyedArchiver archiveRootObject:data toFile:path];
- >
[NSKeyedArchiver archiveRootObject:arry toFile:path];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.