NSCoding和NSCopy
生活随笔
收集整理的這篇文章主要介紹了
NSCoding和NSCopy
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>> ??
很多時候我們都需要將對象序列化,比如將一個對象存入到NSUserDefault 里面去的時候,由于NSUserDefault支持存入的類型有限制,所以很多時候我們需要將NSObject類型的對象轉換成NSData再存入進去。
- (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"]; }當你要進行對象拷貝的時候需要遵循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; }
轉載于:https://my.oschina.net/megan/blog/181463
總結
以上是生活随笔為你收集整理的NSCoding和NSCopy的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux C 数据结构—-循环链表
- 下一篇: C++基础知识(二)—— 变量和数据类型