#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
/*
NSUserDefaults 个人见解 : 存储机制是字典类型
setObject: forKey : 字典的存储数据方法
*/
// 自定义 NSUserDefaults
NSUserDefaults * myUserDefault = [NSUserDefaults standardUserDefaults];
// 给 myUserDefault 赋初值
[myUserDefault setObject:@"中华文化源远流长" forKey:@"中国"]; //
[myUserDefault setObject:@"万国相聚求同存异" forKey:@"U.N"]; // 联合国英文缩写
// 同步更新数据
[myUserDefault synchronize];
// 此时数据已经存储成功 可以在这个程序中任意一个界面取出里面的值
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
@end
**
下面是另一个页面进行数据调用
--------------
**
#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
NSUserDefaults * myUserDefault = [NSUserDefaults standardUserDefaults];
// 取值 后面之所以 key 分别是 中国 U.N 是为了表明 key 是一个字符串 没有太多限制
NSString * myStr = [myUserDefault objectForKey:@"中国"];
NSString * myStrUN = [myUserDefault objectForKey:@"U.N"];
// 取值成功 并打印在控制带上
NSLog(@"%@", myStr);
NSLog(@"%@", myStrUN);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
iOS NSUserDefaults 之最简单存取数据 小Demo
最新推荐文章于 2021-06-03 21:29:29 发布