IOS SDK detailed nscoding protocol

Source: Internet
Author: User

Original blog, reproduced please indicate the source
Http://blog.csdn.net/hello_hwc?viewmode=contents
Welcome to my iOS SDK detailed column
Http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html

Foreword: Nscoding is the protocol that must be followed to encode and decode the model class in iOS, which is necessary if an object is to be archived.

Nscoding to implement two methods
- initWithCoder: //解码- encodeWithCoder://编码

A simple example:

    • Define a model that follows the Nscoding protocol, allowing us to store archived data (directly to plist or nsuserdefaults)

Oc

 @interface mymodel:nsobject<nscoding>@property(Copy,nonatomic)NSString* NAME;@property(nonatomic)intAge@end @implementation mymodel -(Instancetype) Initwithcoder: (Nscoder *) adecoder{if( Self= [SuperInit]) { Self. Name= [Adecoder decodeobjectforkey:@"Kname"]; Self. Age= [Adecoder decodeint32forkey:@"Kage"]; }return  Self;} -(void) Encodewithcoder: (Nscoder *) acoder{[Acoder encodeobject: Self. Nameforkey:@"Kname"]; [Acoder EncodeInt32: Self. Ageforkey:@"Kage"];} -(NSString*) description{return[NSStringstringwithformat:@"name:%@; age:%d ", Self. Name, Self. Age];}

Then it calls

 [Super Viewdidload];    mymodel  * model = [[mymodel  alloc] init];    Model.name = @ "Wenchen" ;    Model.age = 24 ; nsdata  * data  = [nskeyedarchiver  Archiveddatawithrootobject:model];  mymodel  * Unarchedmodel = [nskeyedunarchiver     Unarchiveobjectwithdata:data ];  nslog  (@ "%@" , Unarchedmodel); name : Wenchen; age : 24   

Why do you write description? Because it is easy to debug

Swift implementation

Class Mymodel:nsobject,nscoding {var name:string var age:int32 init (name:string,age:int32) {self. Name= Name Self. Age= Age Super. Init()} Required init (coder Adecoder:nscoder) {self. Name= Adecoder. Decodeobjectforkey("Kname") as! String Self. Age= Adecoder. Decodeintforkey("Kage")} func Encodewithcoder (acoder:nscoder) {Acoder. Encodeobject(Self. Name, Forkey:"Kname") Acoder. Encodeint(Self. Age, Forkey:"Kage")    }}

Call

  letMyModel"Wenchen"24)  letdata = NSKeyedArchiver.archivedDataWithRootObject(model)  letNSKeyedUnarchiver.unarchiveObjectWithData(data) as! MyModel  println("Name:\(unArchedModel.name); Age:\(unArchedModel.age)")

Copyright NOTICE: This article is for bloggers original article, if you need to reprint please indicate the source

IOS SDK detailed nscoding protocol

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.