#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"];
5.1、清除所有数据
NSString*bundle = [[NSBundlemainBundle]bundleIdentifier];
[USER_DEFAULTremovePersistentDomainForName:bundle];
6.1、存取->NSInteger
NSIntegerinteger =1;
[USER_DEFAULTsetInteger:integerforKey:@"integer"];
[USER_DEFAULTintegerForKey:@"integer"];
6.2、存取->float
floatflaot =1.0;
[USER_DEFAULTsetFloat:flaotforKey:@"flaot"];
[USER_DEFAULTfloatForKey:@"flaot"];
6.3、存取->BOOL
BOOLisBool =YES;
[USER_DEFAULTsetBool:isBoolforKey:@"isBool"];
[USER_DEFAULTboolForKey:@"isBool"];
6.4、存取—>Double
doubledoulbe =1.2;
[USER_DEFAULTsetDouble:doulbeforKey:@"doulbe"];
[USER_DEFAULTdoubleForKey:@"doulbe"];
/*NSUserDefaults存储的对象全是不可变。要存储一个NSMutableArray对象,必须先创建一个不可变数组(NSArray)再将它存入NSUserDefaults中去。 */
6.7、存取—>NSArray
NSMutableArray*marry = [NSMutableArrayarrayWithObjects:@"obj_one",@"obj_two",nil];
NSArray*arry = [NSArrayarrayWithArray:marry];
[USER_DEFAULTsetObject:arryforKey:@"arry"];
[NSMutableArrayarrayWithArray:[USER_DEFAULTarrayForKey:@"arry"]];
6.8、存取—>NSDictionary
NSDictionary*diction = [NSDictionarydictionaryWithObjectsAndKeys:@"Jack",@"name",@"18",@"age",nil];
[USER_DEFAULTsetObject:dictionforKey:@"diction"];
[USER_DEFAULTdictionaryForKey:@"diction"];
6.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);