iOS - NSUserDefaults的一些使用方法

 NSUserDefaults 

//将NSUserDefaults的实例化定义成宏
#define USER_DEFAULT [NSUserDefaults standardUserDefaults]
 
 
 
  /*NSUserDefaults是一个单例,适合存储轻量级的本地数据,一些简单的数据(NSString类型的)例如密码,网址等 在整个程序中只有一个实例对象,他可以用于数据的永久保存,一般用来存储简单的信息(支持的数据类型有:NSNumber(NSInteger、float、double),NSString,NSDate,NSArray,NSDictionary,BOOL)。
     */
    
    
    //实例化
    // NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
    //存数据
    //类型-> NSString
    NSString *userName =  @"用户名";
    NSString *userUid =  @"用户id";
    [ USER_DEFAULT setObject:userName forKey: @"name"];
    [ USER_DEFAULT setObject:userUid forKey: @"uid"];
    [ USER_DEFAULT synchronize]; //同步存储到磁盘中(可选)
     //取数据
    NSString *_userName = [USER_DEFAULT objectForKey: @"name"];
    NSString *_userUid = [USER_DEFAULT objectForKey: @"uid"];
    NSLog( @"_userName = %@,_userUid = %@",_userName,_userUid);
    
     //清除指定数据
    [ USER_DEFAULT removeObjectForKey: @"name"];
    [ USER_DEFAULT removeObjectForKey: @"uid"];
     //清除所有数据
    NSString *bundle = [[NSBundle mainBundle] bundleIdentifier];
    [ USER_DEFAULT removePersistentDomainForName:bundle];
    
    
    
     //存储时,除NSNumber类型
    //1、存取->NSInteger
    NSInteger integer = 1;
    [ USER_DEFAULT setInteger:integer forKey: @"integer"];
    [ USER_DEFAULT integerForKey: @"integer"];
     //2、存取->float
     float flaot = 1.0;
    [ USER_DEFAULT setFloat:flaot forKey:@"flaot"];
    [ USER_DEFAULT floatForKey: @"flaot"];
     //3、存取->BOOL
     BOOL isBool =  YES;
    [ USER_DEFAULT setBool:isBool forKey: @"isBool"];
    [ USER_DEFAULT boolForKey: @"isBool"];
     //4、存取—>Double
     double doulbe = 1.2;
    [ USER_DEFAULT setDouble:doulbe forKey: @"doulbe"];
    [ USER_DEFAULT doubleForKey: @"doulbe"];
    
     /*NSUserDefaults 存储的对象全是不可变。要存储一个 NSMutableArray 对象,必须先创建一个不可变数组(NSArray)再将它存入NSUserDefaults中去。
    */
     //7、存取—>NSArray
    NSMutableArray *marry = [NSMutableArray arrayWithObjects: @"obj_one", @"obj_two"nil];
    NSArray *arry = [NSArray arrayWithArray:marry];
    [ USER_DEFAULT setObject:arry forKey: @"arry"];
    [NSMutableArray arrayWithArray:[USER_DEFAULT arrayForKey: @"arry"]];
      //8、存取—>NSDictionary
    NSDictionary *diction = [NSDictionary dictionaryWithObjectsAndKeys: @"Jack", @"name", @"18", @"age"nil];
    [ USER_DEFAULT setObject:diction forKey: @"diction"];
    [ USER_DEFAULT dictionaryForKey: @"diction"];
     //9、存取—>NSData
    
    UIImage *image = [UIImage imageNamed: @""];
    NSData *imagedata = UIImageJPEGRepresentation(image, 1.0); //UIImage-NSData
    [ USER_DEFAULT setObject:imagedata forKey:@"imagedata"];
    NSData *dataimage = [USER_DEFAULT dataForKey: @"imagedata"];
    UIImage *_image = [UIImage imageWithData:dataimage]; //NSData-UIImage
    NSLog( @"获取image->%@",_image);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值