iOS 偏好设置 NSUserDefault
NSUserDefaults类提供了一个与系统进行交互的编程接口,用来存储、恢复应用程序的偏好设置和配置数据等,适合存储轻量级的本地数据。比如保存一个登陆界面的数据,例如用户名、密码之类的。
如果使用plist文件存储,那么就要自己创建文件,读取文件,很麻烦。同时NSUserDefaults的缓存还避免了再每次读取数据的时候都打开数据库的操作.而用NSUserDefaults则不用管这些东西,直接存储或读取就好了。
NSUserDefaults是单例的,同时也是线程安全的。
NSUserDefaults支持的数据类型有:BOOL、NSNumber(Integer、Float、Double)、NSString、NSData、NSDate、NSArray、NSDictionary、NSObject<NSCoding>
注意:
(1)自定义类可以通过NSData的方式进行存储,不过要实现NSCoding这个protocol才行,NSObject<NSCoding>
(2)系统会保存到该应用下的/Library/Preferences/gongcheng.plist文件中。需要注意的是如果程序意外退出,NSUserDefaultsstandardUserDefaults数据不会被系统写入到该文件,所以,要使用[[NSUserDefaultsstandardUserDefaults] synchronize]命令直接同步到文件里,来避免数据的丢失。
(3)从NSUserDefaults返回的值并不是不可改变的,即使是存储的时候使用可变值,当你使用 stringForKey: 方法获取的值,这个值仍然是不可改变的,如果要改变需使用duplicated。
代码如下:
-
//
-
// ViewController.m
-
// NSUserDefaultsDemo
-
//
-
// Created by 555chy on 6/25/16.
-
// Copyright © 2016 555chy. All rights reserved.
-
//
-
-
#import "ViewController.h"
-
-
@interface ViewController ()
-
@end
-
@implementation ViewController
-
-
- (void)viewDidLoad {
-
[super viewDidLoad];
-
// Do any additional setup after loading the view, typically from a nib.
-
-
//NSUserdefaults支持的数据格式有:BOOL、NSNumber(integer、float、double)、NSString、NSDate、NSArray、NSDictionary等
-
-
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
-
-
//写入数据
-
bool sBOOL = YES;
-
NSInteger sInteger = 1;
-
float sFloat = 2.3;
-
double sDouble = 4.5;
-
NSString *sString = @"string";
-
//const char *str = "abcdefg";
-
//NSData *sData = [NSData dataWithBytes:str length:strlen(str)];
-
NSData *sData = [sString dataUsingEncoding:NSUTF8StringEncoding];
-
NSDate *sDate = [NSDate date];
-
NSArray *sArray = [NSArray arrayWithObjects:@"array1", @"array2", @"array3", nil];
-
NSDictionary *sDictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"chy", @"24", @"YES", nil] forKeys:[NSArray arrayWithObjects:@"name", @"age", @"isMale", nil]];
-
-
[userDefaults setBool:sBOOL forKey:@"bool"];
-
[userDefaults setInteger:sInteger forKey:@"integer"];
-
[userDefaults setFloat:sFloat forKey:@"float"];
-
[userDefaults setDouble:sDouble forKey:@"double"];
-
[userDefaults setObject:sString forKey:@"string"];
-
[userDefaults setObject:sData forKey:@"data"];
-
[userDefaults setObject:sDate forKey:@"date"];
-
[userDefaults setObject:sArray forKey:@"array"];
-
[userDefaults setObject:sDictionary forKey:@"dictionary"];
-
-
//这里建议同步存储到磁盘中,但不是必须的,因为数据会自动缓存到内存中
-
[userDefaults synchronize];
-
-
//读取数据
-
BOOL gBOOL;
-
NSInteger gInteger;
-
float gFloat;
-
double gDouble;
-
NSString *gString;
-
NSData *gData;
-
NSDate *gDate;
-
NSArray *gArray;
-
NSDictionary *gDictionary;
-
-
gBOOL = [userDefaults boolForKey:@"bool"];
-
gInteger = [userDefaults integerForKey:@"integer"];
-
gFloat = [userDefaults floatForKey:@"float"];
-
gDouble = [userDefaults doubleForKey:@"double"];
-
gString = [userDefaults stringForKey:@"string"];
-
gData = [userDefaults objectForKey:@"data"];
-
gDate = [userDefaults objectForKey:@"date"];
-
gArray = [userDefaults objectForKey:@"array"];
-
gDictionary = [userDefaults objectForKey:@"dictionary"];
-
-
NSLog(@"gBool = %d", gBOOL);
-
NSLog(@"gInteger = %ld", gInteger);
-
NSLog(@"gFloat = %f", gFloat);
-
NSLog(@"gDouble = %lf", gDouble);
-
NSLog(@"gString = %@", gString);
-
NSLog(@"gData = %@", [[NSString alloc] initWithData:gData encoding:NSUTF8StringEncoding]);
-
NSLog(@"gDate = %@", gDate);
-
NSLog(@"gArray = %@", gArray);
-
NSLog(@"gDictionary = %@", gDictionary);
-
}
-
-
- (void)didReceiveMemoryWarning {
-
[super didReceiveMemoryWarning];
-
// Dispose of any resources that can be recreated.
-
}
-
-
@end
文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/chy555chy/article/details/51759961
- 点赞
- 收藏
- 关注作者
评论(0)