美文网首页
第四节:关于NSUserDefaults介绍

第四节:关于NSUserDefaults介绍

作者: 清杨程 | 来源:发表于2018-09-11 18:50 被阅读7次

       //将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_DEFAULTsetObject:userNameforKey:@"name"];

        [USER_DEFAULTsetObject:userUidforKey:@"uid"];

        [USER_DEFAULTsynchronize];//同步存储到磁盘中(可选)

       //取数据

       NSString*_userName = [USER_DEFAULTobjectForKey:@"name"];

       NSString*_userUid = [USER_DEFAULTobjectForKey:@"uid"];

       NSLog(@"_userName = %@,_userUid = %@",_userName,_userUid);

       //清除指定数据

        [USER_DEFAULTremoveObjectForKey:@"name"];

        [USER_DEFAULTremoveObjectForKey:@"uid"];

       //清除所有数据

       NSString*bundle = [[NSBundlemainBundle]bundleIdentifier];

        [USER_DEFAULTremovePersistentDomainForName:bundle];

       //存储时,除NSNumber类型

       //1、存取->NSInteger

       NSIntegerinteger =1;

        [USER_DEFAULTsetInteger:integerforKey:@"integer"];

        [USER_DEFAULTintegerForKey:@"integer"];

       //2、存取->float

       floatflaot =1.0;

        [USER_DEFAULTsetFloat:flaotforKey:@"flaot"];

        [USER_DEFAULTfloatForKey:@"flaot"];

       //3、存取->BOOL

       BOOLisBool =YES;

        [USER_DEFAULTsetBool:isBoolforKey:@"isBool"];

        [USER_DEFAULTboolForKey:@"isBool"];

       //4、存取—>Double

       doubledoulbe =1.2;

        [USER_DEFAULTsetDouble:doulbeforKey:@"doulbe"];

        [USER_DEFAULTdoubleForKey:@"doulbe"];

       /*NSUserDefaults存储的对象全是不可变。要存储一个NSMutableArray对象,必须先创建一个不可变数组(NSArray)再将它存入NSUserDefaults中去。 */

        //7、存取—>NSArray

       NSMutableArray*marry = [NSMutableArrayarrayWithObjects:@"obj_one",@"obj_two",nil];

       NSArray*arry = [NSArrayarrayWithArray:marry];

        [USER_DEFAULTsetObject:arryforKey:@"arry"];

        [NSMutableArrayarrayWithArray:[USER_DEFAULTarrayForKey:@"arry"]];

        //8、存取—>NSDictionary

       NSDictionary*diction = [NSDictionarydictionaryWithObjectsAndKeys:@"Jack",@"name",@"18",@"age",nil];

        [USER_DEFAULTsetObject:dictionforKey:@"diction"];

        [USER_DEFAULTdictionaryForKey:@"diction"];

        //9、存取—>NSData

       UIImage*image = [UIImageimageNamed:@""];

       NSData*imagedata =UIImageJPEGRepresentation(image,1.0);//UIImage-NSData

        [USER_DEFAULTsetObject:imagedataforKey:@"imagedata"];

       NSData*dataimage = [USER_DEFAULTdataForKey:@"imagedata"];

       UIImage*_image = [UIImageimageWithData:dataimage];//NSData-UIImage

       NSLog(@"获取image->%@",_image);

    相关文章

      网友评论

          本文标题:第四节:关于NSUserDefaults介绍

          本文链接:https://www.haomeiwen.com/subject/sozjdttx.html