NSUserDefaults的一些使用方法
阿新 • • 發佈:2019-01-03
/*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);
*/
//例項化
// 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);