- 類要實(shí)現(xiàn)
NSCoding
協(xié)議
實(shí)現(xiàn)encodeWithCoder
和initWithCoder
方法
- 先把類的instance archive成NSData关划,然后存起來
TGTest *tt = [TGTest new];
tt.num = 3;
NSData *dd = [NSKeyedArchiver archivedDataWithRootObject:tt];
[[NSUserDefaults standardUserDefaults] setObject:dd forKey:@"objtest"];
- 讀數(shù)據(jù)時(shí)柔逼,讀出NSData朱嘴,然后unarchive得到instance须尚。
NSData *dd = [[NSUserDefaults standardUserDefaults] objectForKey:@"objtest"];
@try
{
TGTest *tt= [NSKeyedUnarchiver unarchiveObjectWithData:dd];
}
@catch (NSException *exception)
{
NSLog(@"%@",exception);
}
@finally
{
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者