NSUserDefault存储类
- 类要实现
NSCoding
协议
实现encodeWithCoder
和initWithCoder
方法 先把类的instance archive成NSData,然后存起来
TGTest *tt = [TGTest new]; tt.num = 3; NSData *dd = [NSKeyedArchiver archivedDataWithRootObject:tt]; [[NSUserDefaults standardUserDefaults] setObject:dd forKey:@"objtest"];
读数据时,读出NSData,然后unarchive得到instance。
NSData *dd = [[NSUserDefaults standardUserDefaults] objectForKey:@"objtest"]; @try { TGTest *tt= [NSKeyedUnarchiver unarchiveObjectWithData:dd]; } @catch (NSException *exception) { NSLog(@"%@",exception); } @finally { }
下起雨,也要勇敢前行