我有一个简单的tableview,我用text.field填充它:
@interface ListaDeProdutosTableViewController ()
@property NSMutableArray *produtos;
@end
@implementation ListaDeProdutosTableViewController
现在我想将这些行保存到文件中(以保存数据)并将其读回到tableview。我怎么能这样做?
你能帮帮我吗?谢谢!答案 0 :(得分:0)
不确定要保存的内容,但这里是一个如何将对象保存到文件并稍后阅读的示例:
- (NSString *)cachePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}
- (void)cacheObject:(NSString *)objectId withData:(SomeClass *)objectData{
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:objectData];
NSString *path = [NSString stringWithFormat:@"%@/someobject-%@.cache", [self cachePath], objectId];
[data writeToFile:path atomically:YES];
}
- (SomeClass *)getObjectCache:(NSString *)objectId {
NSString *path = [NSString stringWithFormat:@"%@/someobject-%@.cache", [self cachePath], objectId];
NSData *data = [NSData dataWithContentsOfFile:path];
if (!data) {
return nil;
}
SomeClass *object = [NSKeyedUnarchiver unarchiveObjectWithData:data];
return object;
}