我有一个 NSMutableArray feed.leagues 有两个对象,<MLBLeagueStandings: 0xeb2e4b0>
我想将它写入文件,然后从文件中读取它。这就是我所做的:
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:feed.leagues forKey:@"feed.leagues"];
}
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
self.feed.leagues = [decoder decodeObjectForKey:@"feed.leagues"];
}
return self;
}
-(void)saveJSONToCache:(NSMutableArray*)leaguesArray {
NSString *cachePath = [self cacheJSONPath];
[NSKeyedArchiver archiveRootObject:feed.leagues toFile:cachePath];
NSMutableArray *aArray = [NSKeyedUnarchiver unarchiveObjectWithFile:cachePath];
NSLog(@"aArray is %@", aArray);
}
-(NSString*)cacheJSONPath
{
NSString *documentsDirStandings = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *cacheJSONPath = [NSString stringWithFormat:@"%@/%@_Standings.plist",documentsDirStandings, sport.acronym];
return cacheJSONPath;
}