IOS开发-NSUserDefaults的基本使用,缓存数据实现数据持久化
NSUserDefaults是iOS与 macOS 中的一个存储对象。它用于存储应用程序运行期间和退出后需要保存的数据。
NSUserDefaults的特点:- 基于键值对:使用字符串作为键名存储数据。
- 支持的类型:NSString、NSNumber、NSDate、NSArray、NSDictionary等基本数据结构。
- 存储在本地:数据存储在本地文件中,不会因为应用程序退出而丢失。
- 可以监听变化:可以注册通知监听 NSUserDefaults 对象变化。通过 NSUserDefaults ,我们可以存储:- 应用设置:比如亮度、声音等用户偏好设置
- 配置信息:分辨率、访问权限、主题等配置
- 用户信息:用户名、头像、地区等
- 状态信息:在应用程序重新启动后仍然有效。
使用步骤:
1. 获取默认的用户defaults对象:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
2. 写入要存储的数据:
[defaults setObject:@"用户1" forKey:@"username"];
可以写入的值有:NSString、NSNumber、NSDate、NSArray、NSDictionary。
3. 读取缓存的数据:
NSString *username = [defaults stringForKey:@"username"];
4. 注册defaults变化通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(defaultsDidChange:) name:NSUserDefaultsDidChangeNotification object:nil];
5. 监听通知:
- (void)defaultsDidChange:(NSNotification *)notification { // 注册的defaults值发生变化,会触发这个方法 NSLog(@"修改了%@",[notification.object stringForKey:@"username"]); }
6. 移除通知监听:
[[NSNotificationCenter defaultCenter] removeObserver:self];
完整代码:
AppDelegate.m
// // AppDelegate.m // viewtest // // Created by 001 on 2023/5/9. // #define WYColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0] #import "AppDelegate.h" #import "labelpage.h" #import "imgpage.h" @interface AppDelegate () @end @implementation AppDelegate - (void)defaultsDidChange:(NSNotification *)notification { // 注册的defaults值发生变化,会触发这个方法 NSLog(@"修改了%@",[notification.object stringForKey:@"username"]); } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:@"用户1" forKey:@"username"]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(defaultsDidChange:) name:NSUserDefaultsDidChangeNotification object:nil]; labelpage *labelpageview = [[labelpage alloc] init]; imgpage *imgpageview = [[imgpage alloc] init]; self.window.rootViewController = imgpageview; [self.window makeKeyAndVisible]; NSLog(@"执行了"); return YES; } #pragma mark - UISceneSession lifecycle @end
imgpage.m
// // imgpage.m // blog // // Created by 001 on 2023/7/2. // #import <AVFoundation/AVFoundation.h> @interface imgpage () @end @implementation imgpage - (void)viewDidLoad { [self.view addSubview:imgview]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:@"234" forKey:@"username"]; NSString *username = [defaults stringForKey:@"username"]; NSLog(@"用户名称%@",username); [super viewDidLoad]; } @end
效果图:
注意:
NSUserDefaults 存储的对象全是不可变的(这一点非常关键,弄错的话程序会出bug),
例如,如果我想要存储一个 NSMutableArray 对象,我必须先创建一个不可变数组(NSArray)再将它存入NSUserDefaults中去,代码如下:
NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@ "123",@"234" , nil]; NSArray * array = [NSArray arrayWithArray:mutableArray]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [user setObject:array forKey:@ "记住存放的一定是不可变的" ];
取出数据是一样的是不可变的,要想赋值给可变数组需要用改不可变数组来初始化一个可变数组
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:[user objectForKey:@ "记住存放的一定是不可变的"]];