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

// --- 沙盒路径

 

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

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 先连线 先显示

posted @ 2015-09-06 00:02  熊虎成  阅读(151)  评论(0编辑  收藏  举报