UI_本地存储
来源:互联网 发布:app制作编程教学 编辑:程序博客网 时间:2024/05/29 19:57
本人博客力争每篇博客能以详细的方式描述一个功能实现.如果大家按照博客中的示范代码没有实现功能,请通知本人,本人会以最快时间修改.
UI 本地存储
控件关键字: NSSearchPathForDirectoriesInDomains, writeToFile, NSKeyedArchiver, NSKeyedUnarchiver)
逻辑顺序
// 1. 拼接一个文件的路径
// 2. 获取documents的路径
// 3. 写入本地
写入本地分对系统的简单对象和自定义的复杂对象两种情况进行处理
系统自定义的简单对象(NSString, NSArray, NSDictionary.....)
1. 字符串写入本地 , 读取
2. 数组写入本地, 读取
3. 字典写入本地, 读取
4. NSData NSSet... , 读取
自定义对象
使用归档和反归档类进行写入和读取
// 本地存储 // 找到沙盒的路径 // 返回值: 数组 // 参数1 : 搜索沙盒中的哪个文件夹 // 参数2: 要搜索设备的位置(手机上只选userDomainMask) // 参数3: 相对路径(NO)/绝对路径(YES) NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSLog(@"数组路径 %@", paths); // 沙盒中的文件 /* 1. Documents文件夹: 用户要存储的内容都写在这个文件夹里, 一般来说是用户对应用程序的设置, 比如, 夜间模式. 一旦APP使用了苹果的iCloud服务,APP会自动把documents文件中的所有内容上传到服务器. 2. Library文件夹: 2.1 Caches文件夹: 缓存文件夹,所有缓存的文件都推荐写在这里 (Json数据, 图片, 音频, 视频....) 2.2 Preferences 文件夹: 给开发者使用, 保存应用程序的状态和设置. 3. tmp文件夹: 临时文件夹, 保存临时数据. 4. 应用程序包: 里面所有的东西都是只读的. 保存的APP的所有代码,资源,就是NSBundel */ // 本地存储 // 1. 系统定义的简单对象,(NSString, NSArray, NSDictionary.....) // 1.1 字符串写入本地 // 定义字符串 NSString *str = @"长城"; // 拼接一个文件的路径 // 获取documents的路径 NSString *documentPath = [paths lastObject]; NSString *strPath = [documentPath stringByAppendingString:@"/china.txt"]; // 写入本地 // 参数1 : 保存文件的路径 // 参数2 : 保证写入的时候的数据安全 // 参数3 : 对写入的内容进行编码 // 参数4 : 错误信息: NSError *error = nil; BOOL result = [str writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:&error]; NSLog(@"写入结果: %d", result); // 1.2 数组写入本地 NSArray *arr = [NSArray arrayWithObjects:@"北京", @"上海", @"广州", @"深圳", nil]; // 拼接路径 NSString *arrPath = [documentPath stringByAppendingPathComponent:@"城市.xml"]; BOOL arrResult = [arr writeToFile:arrPath atomically:YES]; NSLog(@"数组写入结果: %d", arrResult); // 1.3 字典写入本地(plist) NSDictionary *dic = [NSDictionary dictionaryWithObject:@"北京" forKey:@"首都"]; NSString *dicPath = [documentPath stringByAppendingPathComponent:@"字典.plist"]; BOOL dicResult = [dic writeToFile:dicPath atomically:YES]; NSLog(@"字典写入结果: %d", dicResult); // 对字典/数组的写入, 后缀一般都是xml/plist // NSData NSSet.... // 从本地读取数据 // 字符串 NSString *readStr = [[NSString alloc] initWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:nil]; NSLog(@"%@", readStr); // 数组 NSArray *readArr = [NSArray arrayWithContentsOfFile:arrPath]; NSLog(@"%@", readArr);
自定义对象的本地存储
Boss为自定义的类,父类为NSObject
本人博客力争每篇博客能以详细的方式描述一个功能实现.如果大家按照博客中的示范代码没有实现功能,请通知本人,本人会以最快时间修改.
0 0
- UI_本地存储
- 本地存储
- 本地存储
- 本地存储
- 本地存储
- 本地存储
- 本地存储
- 本地存储
- 本地存储
- 本地存储
- 本地存储
- 本地存储
- 本地存储
- 本地存储
- 本地存储
- 本地存储
- 本地存储
- 本地存储
- 谈谈Winform程序的界面设计
- Hibernate入门体验
- Could not reload resource bundles java.lang.NoSuchFieldException: cacheList
- Linux的文本编辑器
- js setTimeOut() 方法传参
- UI_本地存储
- css 实现自定义下拉框样式
- jQuery(三) jQuery Html
- RNN 2:反馈神经网络的解决OXR问题
- NSNotification 通知传值
- 程序设计作业报告项目四——“撞错”体验
- m版开发ing
- eclipse安装Android模拟器genymotion及其插件
- Linux网络配置