iOS数据持久化之NSUserDefaults简介
NSUserDefaults是偏好设置,一般专门用来保存应用程序的配置信息等。
NSUserDefaults是直接继承自NSObject的:@interface NSUserDefaults : NSObject
/*!
+standardUserDefaults returns a global instance of NSUserDefaults configured to search the current application's search list.
*/
#if FOUNDATION_SWIFT_SDK_EPOCH_AT_LEAST(8)
@property (class, readonly, strong) NSUserDefaults *standardUserDefaults;
#endif
可以看出NSUserDefaults是一个单例,通过[NSUserDefaults standardUserDefaults]方法获得它的一个实例。
下面来对NSUserDefaults的方法大致的分一下类
- (void)setObject:(nullable id)value forKey:(NSString *)defaultName
- (nullable id)objectForKey:(NSString *)defaultName
这一对方法是我们日常进行NSUserDefaults设置值和读取值得最常用的方法,而且从(nullable id)value和- (nullable id)可以看出,value及objectForKey返回值必须是继承NSObject的对象。
- (nullable id)objectForKey:(NSString *)defaultName有很多根据返回值类型不同的替代方法,如下:
- (nullable NSString *)stringForKey:(NSString *)defaultName
- (nullable NSArray *)arrayForKey:(NSString *)defaultName
- (nullable NSDictionary<NSString *, id> *)dictionaryForKey:(NSString *)defaultName
- (nullable NSData *)dataForKey:(NSString *)defaultName
- (nullable NSArray*)stringArrayForKey:(NSString *)defaultName
- (nullable NSURL *)URLForKey:(NSString *)defaultName
- (NSInteger)integerForKey:(NSString *)defaultName
- (float)floatForKey:(NSString *)defaultName
- (double)doubleForKey:(NSString *)defaultName
- (BOOL)boolForKey:(NSString *)defaultName
详细的说明这里就不说了,大家看文档吧,特别简单,这里很可喜的是给我们提供了返回基础数据类型的方法,可以省去我们进行类型转换。
- (void)setObject:(nullable id)value forKey:(NSString *)defaultName同样有很多根据传入值类型不同的替代方法,如下:
- (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName
- (void)setFloat:(float)value forKey:(NSString *)defaultName
- (void)setDouble:(double)value forKey:(NSString *)defaultName
- (void)setBool:(BOOL)value forKey:(NSString *)defaultName
- (void)setURL:(nullable NSURL *)url forKey:(NSString *)defaultName
其中NSInteger、float、double、BOOL这几个方法会帮我们将值转为NSNumber,然后调用- (void)setObject:(nullable id)value forKey:(NSString *)defaultName方法,所以以后不要说NSUserDefaults不可以直接设置基础数据类型必须转成NSNumber才可以,只不过方法不是直接调用- (void)setObject:(nullable id)value forKey:(NSString *)defaultName而已
对某个值得移除可以通过下面这个方法:
- (void)removeObjectForKey:(NSString *)defaultName
来举个栗子:
defaults = [NSUserDefaults standardUserDefaults];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"Preferences"];
NSLog(@"Preferences == %@",path);
UIButton *writeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[writeBtn setTitle:@"写入NSUserDefaults" forState:UIControlStateNormal];
writeBtn.backgroundColor = [UIColor greenColor];
[writeBtn addTarget:self action:@selector(writeUserDefaults) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:writeBtn];
WeakSelf;
[writeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(weakSelf.view);
make.top.offset(80);
make.left.offset(20);
make.right.offset(-20);
make.height.offset(40);
}];
UIButton *readBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[readBtn setTitle:@"读取NSUserDefaults" forState:UIControlStateNormal];
readBtn.backgroundColor = [UIColor greenColor];
[readBtn addTarget:self action:@selector(readUserDefaults) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:readBtn];
[readBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(weakSelf.view);
make.top.equalTo(writeBtn.mas_bottom).with.offset(30);
make.left.offset(20);
make.right.offset(-20);
make.height.offset(40);
}];
- (void)writeUserDefaults
{
NSString *name = @"王小明";
NSInteger age = 25;
BOOL isMarried = YES;
float money = 14356.23;
[defaults setObject:name forKey:@"name"];
[defaults setInteger:age forKey:@"age"];
[defaults setBool:isMarried forKey:@"isMarried"];
[defaults setFloat:money forKey:@"money"];
[defaults synchronize];
}
界面- (void)readUserDefaults
{
NSString *name = [defaults stringForKey:@"name"];
NSInteger age = [defaults integerForKey:@"age"];
BOOL isMarried = [defaults boolForKey:@"isMarried"];
float money = [defaults floatForKey:@"money"];
NSString *alertStr = [NSString stringWithFormat:@"name:%@ age:%ld isMarried:%d money:%2f",name, age, isMarried, money];
NSLog(@"%@", alertStr);
}
首先我们先进入Preferences文件夹,初始状态下该文件夹下没有任何文件,点击“写入NSUserDefaults”按钮,执行写入,会发现在Preferences文件夹生成了一个以Bundle Identifier为名的plist文件,打开如下:
plist文件内容