一、NSUserDefault介绍
iOS下可以使用NSUserDefault、NSKeyedArchiver、sqlite、CoreData几种常用的方式来存储数据,其中NSUserDefaults用来存储类似用户的配置等这些的数据,后两者用户存储大批量和比较复杂的数据。
NSUserDefault 基本上支持所有的原生数据类型NSString、 NSNumber、NSDate、 NSArray、NSDictionary、BOOL、NSInteger、NSFloat等等。如果要存放自定义的对象(如自定义的类对象),则必须将其转换成NSData存储。
二、NSUserDefault存储和获取数据
//存储数据
NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
[user setObject:currentVersion forKey:CZVersionKey];
//需要注意的是,NSUserDefaults是定时把缓存中的数据写入磁盘的,而不是即时写入,为了防止在写完NSUserDefaults后程序退出导致的数据丢失,可以在写入数据后使用synchronize强制立即将数据写入磁盘:
//运行下面的语句后,NSUserDefaults中的数据即被写入到.plist文件中,如果是在模拟器上运行程序,可以在Mac的/Users/YOUR-USERNAME/Library/Application Support/iPhone Simulator/4.1/Applications/YOUR-APP-DIR/Library/Prefereces目录下面找到一个文件名为YOUR-Bundle_Identifier.plist的plist文件,用Xcode打开该文件,可以看到刚才写入的数据。
[user synchronize];
//删除数据
NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
[user removeObjectForKey:CZVersionKey];
//获取数据
NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
NSString * lastVersion = [user objectForKey:CZVersionKey];
三、转NSData进行存储
//UIImage 实例不能进行存储
-(void)imageUserDefault{
UIImage * image = [UIImage imageNamed:@"1"];
#if 0
//报错 - [User Defaults] Attempt to set a non-property-list object <UIImage: 0x60800008ece0>, {0, 0} as an NSUserDefaults/CFPreferences value for key image
[self setObject:image withKey:@"image"];
#else
//UIImage 实例需要转换为NSData进行存储
NSData *data = nil;
if (UIImagePNGRepresentation(image) == nil) {
data = UIImageJPEGRepresentation(image, 1);
} else {
data = UIImagePNGRepresentation(image);
}
[self setObject:data withKey:@"image"];
NSData * getImageData = [self getObjectWithKey:@"image"];
NSLog(@"getImageData = %@",getImageData);
UIImage * getImage = [UIImage imageWithData:getImageData];
NSLog(@"getImage = %@",getImage);
#endif
}
//自定义对象 不能进行存储
-(void)customClassUserDefault{
MYView * myView = [[MYView alloc] initWithFrame:self.view.bounds];
#if 0
//报错 - Attempt to set a non-property-list object <MYView: 0x7f8b1060b810; frame = (0 0; 375 667); layer = <CALayer: 0x608000034a60>> as an NSUserDefaults/CFPreferences value for key myView
//要想对自定义对象进行存储需要将自定义对象进行归档
[self setObject:myView withKey:@"myView"];
#else
NSData * myData = [NSKeyedArchiver archivedDataWithRootObject:myView];
[self setObject:myData withKey:@"myData"];
NSData * getMyData = [self getObjectWithKey:@"myData"];
NSLog(@"getMyData = %@",getMyData);
MYView * getMyView = [NSKeyedUnarchiver unarchiveObjectWithData:getMyData];
NSLog(@"getMyView = %@",getMyView);
//要想MYView实例的属性能存入需要实现coding协议
NSLog(@"getMyView.mySring = %@",getMyView.myString);
#endif
}
四、利用NSUserDefault判断程序启动
//抽出一个工具类在.h文件中代码如下:
#import <Foundation/Foundation.h>
@interface FirstLaunchTool : NSObject
+(BOOL)isFirstLaunchOfToday;
+(BOOL)isFirstLaunch;
@end
//.m文件中代码如下:
+(BOOL)isFirstLaunch{
NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
NSString * firstStr = [user objectForKey:@"firstLaunch"];
if (!firstStr) {
[user setObject:@"firstLaunch" forKey:@"firstLaunch"];
return YES;
}
else{
return NO;
}
}
+(BOOL)isFirstLaunchOfToday{
//获取今天的时间
NSDate * date = [NSDate date];
NSDateFormatter * formatter = [[ NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMdd";
NSString * today = [formatter stringFromDate:date];
//与存储时间对比
NSUserDefaults * user =[NSUserDefaults standardUserDefaults];
NSString * yesterday = [user objectForKey:@"today"];
if ([today isEqualToString:yesterday]) {
NSString * countStr = [user objectForKey:@"count"];
NSUInteger count = [countStr integerValue];
count+=1;
[user setObject:[NSString stringWithFormat:@"%lu",count] forKey:@"count"];
[user synchronize];
return NO;
}
else{
[user setObject:today forKey:@"today"];
[user setObject:@"0" forKey:@"count"];
[user synchronize];
return YES;
}
}
//在Appdelegate.m文件中添加如下代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//判断是不是第一次启动程序
BOOL bl = [FirstLaunchTool isFirstLaunch];
if (bl) {
NSLog(@"第一次启动");
}
else{
NSLog(@"不是第一次启动");
}
//判断今天是不是第一次启动程序。
BOOL bl1 = [FirstLaunchTool isFirstLaunchOfToday];
if (bl1) {
NSLog(@"今天第一次登陆");
}
else{
NSLog(@"不是今天第一次登陆");
}
return YES;
}
参考资料: