数据存取,归档和解档,偏好设置

本文介绍了iOS开发中沙盒的概念及其使用方法,包括获取沙盒根目录、文档目录及临时目录等。此外还详细讲解了plist文件的读写操作、用户偏好设置的使用以及归档解档的过程。

// --- 沙盒路径

 

// 如何获取沙盒的根目录

NSString* path = NSHomeDirectory();

 

// 快速查看沙盒目录

// SimPholders2

 

// 如何获取 doc 路径

NSString* docpath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

 

// 获取tmp路径

NSString* tmpPath = NSTemporaryDirectory();

 

// --- plist存储

 

// plist

// array(数组)

NSArray* array = @[ @"abc", @"传智播客" ];

[array writeToFile:filePath atomically:YES];

// dict(字典)

NSDictionary* dict = @{ @"key111" : @"value" };

[dict writeToFile:filePath atomically:YES];

}

 

// plist

// 取数组

NSArray* array = [NSArray arrayWithContentsOfFile:filePath];

NSLog(@"%@", array);

// 取字典

NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:filePath];

NSLog(@"%@", dict);

 

// --- 偏好设置

// 1.偏好设置不需要获取目录(不需要关心文件名)

// 2.NSUserDefaults 是一个单例

// 3.NSUserDefaults 实际上就是苹果帮我们封装好的一个字典

// 4.默认创建的文件名字叫做 bundleid + plist

// 5.字典怎么用 NSUserDefaults 就怎么用

 

//  记得 加上 同步!!!!!!!!!!!

[ud synchronize];

 

// 获取偏好设置存储的对象

//

NSUserDefaults* ud = [NSUserDefaults standardUserDefaults];

[ud setObject:@"传智播客" forKey:@"key"];

[ud setBool:YES forKey:@"isOn"];

 

//

NSUserDefaults* ud = [NSUserDefaults standardUserDefaults];

NSString* str = [ud objectForKey:@"key"];

NSLog(@"%@", str);

 

// --- 归档解档

// 归档()

[NSKeyedArchiver archiveRootObject:t toFile:filePath];

 

// 解档()

Teacher* t = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

 

// filePath 文件名 随便取(保证 存取是一个路径就可以)

 

// 步骤:

// 1.遵守<NSCoding>的协议

 

// 2.实现 encode with coder

// 2.1 告诉系统归档哪些属性

 

// 3.实现 init with coder

// 3.1 判断 self = [super init]

// 3.2 告诉系统需要解档哪些属性(记得给属性赋值)

 

// 归档数组的时候

// 系统会自动判断里面的元素是什么类型的

// 会去自动寻找当前类型的 <NSCoding> 的两个方法

 

// --- TabBarController

// 1.添加子控制器

[tab addChildViewController:vc1];

 

// 2.添加子控制器

tab.viewControllers = @[ vc1, vc2, vc3 ];

 

// 设置 标题 图片 提醒数字

// vc1.tabBarItem

 

// tabbar的生命周期 nav 不一样

 

// tabbar 先添加 先显示

// tabbar 先连线 先显示

转载于:https://www.cnblogs.com/xhc1263478959/p/4784260.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值