NSCoding和NSCopy
生活随笔
收集整理的這篇文章主要介紹了
NSCoding和NSCopy
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
很多時(shí)候我們都需要將對(duì)象序列化,比如將一個(gè)對(duì)象存入到NSUserDefault 里面去的時(shí)候,由于NSUserDefault支持存入的類(lèi)型有限制,所以很多時(shí)候我們需要將NSObject類(lèi)型的對(duì)象轉(zhuǎn)換成NSData再存入進(jìn)去。
- (id)initWithCoder:(NSCoder *)aDecoder {self = [super init];if (self) {self.country = [aDecoder decodeObjectForKey:@"country"];self.city = [aDecoder decodeObjectForKey:@"city"];self.region = [aDecoder decodeObjectForKey:@"region"];self.street = [aDecoder decodeObjectForKey:@"street"];self.location = [aDecoder decodeObjectForKey:@"location"];}return self; }- (void)encodeWithCoder:(NSCoder *)aCoder {[aCoder encodeObject:_country forKey:@"country"];[aCoder encodeObject:_city forKey:@"city"];[aCoder encodeObject:_region forKey:@"region"];[aCoder encodeObject:_street forKey:@"street"];[aCoder encodeObject:_location forKey:@"location"]; }當(dāng)你要進(jìn)行對(duì)象拷貝的時(shí)候需要遵循NSCopy協(xié)議
- (id)copyWithZone:(NSZone *)zone {id copy = [[[self class] alloc] init];if (copy) {[copy setId:[self.id copyWithZone:zone]];[copy setNickName:[self.nickName copyWithZone:zone]];}return copy; }
轉(zhuǎn)載于:https://my.oschina.net/megan/blog/181463
總結(jié)
以上是生活随笔為你收集整理的NSCoding和NSCopy的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux C 数据结构—-循环链表
- 下一篇: 【金融量化】期货中的成交量和持仓量指标