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
這一對方法是我們?nèi)粘_M行NSUserDefaults設置值和讀取值得最常用的方法谈山,而且從(nullable id)value和- (nullable id)可以看出何吝,value及objectForKey返回值必須是繼承NSObject的對象。
- (nullable id)objectForKey:(NSString *)defaultName有很多根據(jù)返回值類型不同的替代方法继谚,如下:
- (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
詳細的說明這里就不說了烈菌,大家看文檔吧,特別簡單犬庇,這里很可喜的是給我們提供了返回基礎數(shù)據(jù)類型的方法僧界,可以省去我們進行類型轉(zhuǎn)換。
- (void)setObject:(nullable id)value forKey:(NSString *)defaultName同樣有很多根據(jù)傳入值類型不同的替代方法臭挽,如下:
- (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這幾個方法會幫我們將值轉(zhuǎn)為NSNumber葬荷,然后調(diào)用- (void)setObject:(nullable id)value forKey:(NSString *)defaultName方法涨共,所以以后不要說NSUserDefaults不可以直接設置基礎數(shù)據(jù)類型必須轉(zhuǎn)成NSNumber才可以,只不過方法不是直接調(diào)用- (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文件夾宠漩,初始狀態(tài)下該文件夾下沒有任何文件举反,點擊“寫入NSUserDefaults”按鈕,執(zhí)行寫入扒吁,會發(fā)現(xiàn)在Preferences文件夾生成了一個以Bundle Identifier為名的plist文件火鼻,打開如下: