Mu// 创建一个model类
// Boss.h
#import <Foundation/Foundation.h>
@interface Boss : NSObject<NSCoding>
// 要使一个类的对象可以写入本地,需要实现一个NSCoding协议
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;
// 魔王类
@property(nonatomic, retain)NSString *name;
@property(nonatomic, retain)NSString *sex;
@property(nonatomic, assign)NSInteger number;
@end
// Boss.m
#import "Boss.h"
@implementation Boss
- (void)encodeWithCoder:(NSCoder *)aCoder
{
// 编码的方法 将对象的所有数据写到一起 方便存储
// 将某一条数据编码
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.sex forKey:@"sex"];
[aCoder encodeInteger:self.number forKey:@"number"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
// 解码的方法
//
self.name = [aDecoder decodeObjectForKey:@"name"];
self.sex = [aDecoder decodeObjectForKey:@"sex"];
self.number = [aDecoder decodeIntegerForKey:@"number"];
}
return self;
}
@end
// MainViewController.h
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
@end
// MainViewController.m
#import "MainViewController.h"
#import "Boss.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// 本地存储
// 找到沙盒的路径
// 返回值: 数组
// 参数1: 搜索沙盒中的哪个文件夹
// 参数2: 要搜索设备的位置
// 参数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. temp文件夹:临时文件夹 , 保存临时数据
4. 应用程序包: 里面所有的东西都是只读的,保存的app的所有的代码,资源
NSBundle
*/
// 本地存储
// 1.系统定义的简单对象(NSString, NSArray, NSDictionary...)
// 1.1 字符串写入本地
// 定义字符串
NSString *str = @"大卫和天伟一起约";
// 拼接文件的路径
// 获取documents的路径
NSString *documentPath = [paths lastObject];
NSString *strPath = [documentPath stringByAppendingString:@"/贵川.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字典写入本地
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value", @"key", @"value1", @"key1", nil];
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 *boss = [[Boss alloc] init];
boss.name = @"老蒋";
boss.sex = @"挖掘机";
boss.number = 2;
// 归档类 将一个实现了NSCoding协议的对象 写入本地
NSString *bossPath = [documentPath stringByAppendingPathComponent:@"boss.aaaa"];
BOOL bossResult = [NSKeyedArchiver archiveRootObject:boss toFile:bossPath];
NSLog(@"老蒋的结果:%d", bossResult);
// 反归档类
Boss *bossBack = [NSKeyedUnarchiver unarchiveObjectWithFile:bossPath];
NSLog(@"%@", bossBack.name);
// 如果系统的简单对象(容器对象)中,包含有你定义的复杂对象
// 这些简单的容器是不能直接利用writeToFile方法写入本地的
// 需要使用归档和反档类 写入本地
// 所以说,归档类 是写入本地文件的通用方式。
}
本地存储
最新推荐文章于 2024-07-18 22:15:17 发布