Afnetworking Knowledge Point 1--nscoding nssecurecoding

Source: Internet
Author: User

The process of nsdata  an object to NSData is called serialization, and NSData to the object is called deserialization. Any object that goes to NSData needs to follow a protocol, that is, nscoding.  nscoding is an extremely simple and convenient way to store data on iOS and Mac OS, turning model objects directly into a file, and then reloading the files into memory, without the need for any file parsing and serialization logic. By extending your data classes to support the encode and decode functions.   nscoding is a simple protocol that includes:-initwithcoder: -encodewithcoder: Classes that follow the Nscoding protocol can be serialized and deserialized.   nscoder is an abstract class that cannot be instantiated by an abstract class Nskeyedunarchiver   read objects from a binary stream nskeyedarchiver        Write objects into a binary stream   example: @interface student:nsobject<nscopying,nscoding>  @property (nonatomic, strong ) NSString *studentname;  @property (nonatomic, strong) NSString *score;  @end   @implementation Student  //decoding-(instancetype) Initwithcoder: (Nscoder *) adecoder{    if (self = [super init]) {  & nbsp;     self.studentname = [Adecoder decodeobjectforkey:@ "Studentname"];         Self.score = [adecoder decodeobjectforkey:@ "Score"];   }    Return Self;}  //encoding-(void) Encodewithcoder: (Nscoder *) acoder{    [Acoder encodeObject:self.studentName Forkey : @ "Studentname"];    [Acoder encodeObject:self.score forkey:@ "Score"];} @end   nsdata *data = [Nskeyedarchiver archiveddatawithrootobject:student];    student * Student2 = [Nskeyedunarchiver unarchiveobjectwithdata:data];   This establishes a bridge between any object and NSData.    with NSData for storing and deep copying of nscoding classes and serializing data, there are two benefits 1. Serialized data can be stored directly 2. Serialized data is easy to make a full copy   turn an object NSData and NSData Revert to the new object. Nsdata *data = [nskeyedarchiver archiveddatawithrootobject:oldcontactsarray]; Nsmutablearray *newcontactsarray = [nskeyedunarchiver unarchiveobjectwithdata:data];   Nssecurecoding the problem with using nscoding to read and write user data files is that by encoding all the classes into a file, it means giving the file access to the instance class within your app.  nssecurecoding In addition to specifying the key and the class of the object to decode when decoding, if the required class does not match the class of the object decoded from the file, Nscoder throws an exception, which means the data has been tampered with. Classes that conform to the Nssecurecoding protocol and re-write-initwithcoder should use-decodeobjectofclass:forkey:  instead of-decodeobjectforkey:  Example-(instancetype) Initwithcoder: (Nscoder *) adecoder{    if (self = [super init]) {//         self.studentname = [Adecoder decodeobjectforkey:@ "Studentname"];//         Self.score = [adecoder decodeobjectforkey:@ "Score"];         self.studentname = [Adecoder decodeobjectofclass:[nsstring class] forkey:@ "Studentname"];         Self.score = [Adecoder decodeobjectofclass:[nsstring class] forkey:@ "score"];    }    return self;}  nscoding is the way in which an object is archived to a file system or copied to another address space in the underlying class library.   If you use-decodeobjectforkey: to decode an object into an actual object, there is no guarantee that the object created is the expected result.     extensions: about using runtime custom implementation nscoding http://blog.jobbole.com/67655/http://iosdevelopertips.com/ Cocoa/nscoding-without-boilerplate.html

Afnetworking knowledge points 1--nscoding nssecurecoding

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.