Lesson 0-3 Objective-C basic

2019-08-08  本文已影响0人  快乐捣蛋鬼

15. 数据持久化

数据持久化方式:

NSArray *array = @[@"one", @"two", @"three"];
// 将array写入text.plist文件中
[array writeToFile: @''/Users/ziyo/Desktop/plist/test.plist/" automically: YES];

// 将dic写入text.plist文件中
NSDictionary *dict = @{@"name": @"Tom"};
[dic writeToFile: @''/Users/ziyo/Desktop/plist/test.plist/" automically: YES];

// 取出text.plist文件 array 的内容
NSArray *resultArray = [NSArray arrayWithContentsOfFile: @"/Users/ziyo/Desktop/plist/test.plist"];
//  取出text.plist文件 dic 的内容
NSArray *resultDic = [NSDictionary dictionaryWithContentsOfFile: @"/Users/ziyo/Desktop/plist/test.plist"];



NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *string = @"www.baidu.com";
// 存储
[defaults setObject:string forKey:@"website"];
// 立刻同步
[defaults synchronize]; 
// 读取
NSString *resultString = [defaults objectForKey:@"website"];
// 移除
[defaults removeObjectForKey:@"website"];
// 调用所有的键值对
NSDictionary *dict = defaults.dictionaryRepresentation;
// 对Person的3个属性进行编码,在进行归档的时候由系统调用
- (void) encodeWithCoder: (NSCoder *)aCoder {
    [aCoder encodeObject: _name forKey:@"name"];
    [aCoder encodeObject: _age forKey:@"age"];
    [aCoder encodeObject: _sex forKey:@"sex"];
}

在解档的时候不可能把二进制数据转换成person对象,需要用到一个反编码的过程,通过重写initWithCoder完成

// 解档之后要用的方法
- (instancetype) initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        _name = [aDecoder decodeObjectForKey:@"name"];
        _age = [aDecoder decodeObjectForKey:@"age"];
        _sex = [aDecoder decodeObjectForKey:@"sex"];
    }
    return self;
}

// 归档&&解档
Person *person =[[Person alloc] init ];
person.name = @"Tom";
person.age = 12;
person.sex = @"male";

// 归档-编码,完成数据转换,转成二进制
NSData *personData = [NSKeyedArchiver archivedDataWithRootObject:person];
// 归档-写入本地文件
personData writeToFile:@"/Users/ziyo/Desktop/归档/personData" automically: YES];

// 解档 - 读取文件
personData = [NSData dataWithContentsOfFile:@"/Users/ziyo/Desktop/归档/personData"];
// 解档 - 反编码,二进制转成。。。
Person *resultPerson = [NSKeyedUnarchiver unarchiveObjectWithData: personData];

16.深拷贝和浅拷贝

深拷贝和浅拷贝

浅拷贝:只进行对象地址的拷贝,效率很高
深拷贝:直接去内存中开辟一个新的对象的空间地址,创建一个对象的副本实体,person2去指向新的Person对象副本
对不可变的对象进行copy的时候,进行的是浅考本,新增一个指针指向原来的内存地址。
对可变的对象进行copy的时候,是将可变对象拷贝到不可变的对象,依旧是浅拷贝
对可变/不可变的对象进行mutable copy的时候,都是深拷贝,且拷贝的结果一定都是可变的

NSMutableString *mutableStringCopy = [string mutableCopy];

17.JSON 数据

JSON:相比XML,数据结构简单,速度快,可读性差
分清楚Json对象和Json数据(二进制)的差别

// 创建Json对象:字典和数组
NSDictionary *jsonDict = @{@"name": @"Tom", @"age", @22"};
// 将Json对象转为Json数据
NSData *data = [NSJSONSerialization dataWithJSONObject: jsonDict options:0 error: nil];

// 构建Json字符串
NSString *jsonString = @"{\"name\": \"Tom\"}";
// 将json字符串转成二进制数据
NSData *jsonData = [jsonString dataUsingEncoding: NSUTF8StringEncoding];
// 将二进制数据转成Json对象
NSDictionary *resultDict = [NSJSONSerialization JSONObjectWithData:jsonData options: 0 error:nil];

如果不确定转换结果是NSDictioary还是NSArray,可以用泛型id来代替

18.KVC 键值编码

给某一个类的对象完成属性的赋值:

19.RunTime基础

Runtime概述

Runtime的使用

上一篇下一篇

猜你喜欢

热点阅读