码迷,mamicode.com
首页 > 其他好文 > 详细

NSUserDefault存储类

时间:2017-01-14 11:21:03      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:roo   root   try   user   exce   方法   test   读数   standard   

  1. 类要实现NSCoding协议
    实现encodeWithCoderinitWithCoder方法
  2. 先把类的instance archive成NSData,然后存起来

    TGTest *tt = [TGTest new];
    tt.num = 3;
    NSData *dd = [NSKeyedArchiver archivedDataWithRootObject:tt];
    [[NSUserDefaults standardUserDefaults] setObject:dd forKey:@"objtest"];
    
  3. 读数据时,读出NSData,然后unarchive得到instance。

    NSData *dd = [[NSUserDefaults standardUserDefaults] objectForKey:@"objtest"];
    @try
    {
        TGTest *tt= [NSKeyedUnarchiver unarchiveObjectWithData:dd];
    }
    @catch (NSException *exception)
    {
        NSLog(@"%@",exception);
    }
    @finally
    {
    
    }
    

NSUserDefault存储类

标签:roo   root   try   user   exce   方法   test   读数   standard   

原文地址:http://www.cnblogs.com/huahuahu/p/NSUserDefault-cun-chu-lei.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!