Objective-c (10) Abstract Parent Class---protocol

Source: Internet
Author: User
Tags decode all

    • Definition and use of the Protocol

What is an agreement?

Can be used to declare a whole bunch of methods (cannot declare member variables)

As long as a class follows this protocol, it is equivalent to having all the method declarations in this Protocol

As long as the parent class adheres to a protocol, the subclass also adheres to the

Add protocol header file: objective-c file-> protocol


The protocol is similar to an interface in Java, similar to an abstract class in C + +


Define the protocol:

@protocol protocol name < parent protocol Name 1, parent agreement name 2, ....//method declaration list @end


Limitations of the methods in the protocol:

@required The subsequent method requires that a specific class be implemented

@optional Subsequent methods require that the implementation be selected in a specific class

Follow the agreement:

@interface Class Name: Parent class < protocol name 1, protocol name 2, ...//@end


Agreements can also be adhered to, equivalent to the expansion of existing agreements


Base protocol NSObject:

This protocol declares the basic methods such as memory management, and the custom protocol must follow this base protocol

The object properties of the class (compound) need to indicate which protocols the property follows:

@property (Nonatomic, Strong) class name < protocol name > * attribute name, @property (nonatomic, strong) id< protocol name > attribute name;

In other words, the class name < protocol name > represents a deterministic type

The protocol can be defined in a separate. h file, or in a file of a class


    • Nscopying protocol

Create a copy of the object, using the copy or Mutablecopy method to create a copy of the object

-(ID) copy-(ID) mutablecopy


Shallow copy

Copy of pointer, source object and copy object to the same object

The reference count of the object is +1, which is equivalent to doing a retain. (Memory management)

Deep copy

Copy of content, source object and replica object pointing to different objects

The reference count of the source object is not changed, and the reference count for the copy is 1

Objects must follow the Nscopying protocol if they want to call the Copy method, and implement the method:

-(ID) Copywithzone: (Nszone *) zone

Such as:

-(ID) Copywithzone: (Nszone *) zone {Amperson * p = [[[Self class] allocwithzone:zone] init];    P.age = Self.age;    P.name = [self.name copy]; return p;}


object to invoke the Mutablecopy method must follow the Nsmutablecopying protocol and implement the method:

-(ID) Mutablecopywithzone: (Nszone *) zone


    • Nscoding protocol

Nskeyedarchiver Filing Device

+ (BOOL) Archiverootobject: (ID) rootobject tofile: (NSString *) path+ (NSData *) Archiveddatawithrootobject: (ID) Rootobject


Nskeyedunarchiver Gear-out device

+ (ID) unarchiveobjectwithdata: (NSData *) data+ (ID) unarchiveobjectwithfile: (NSString *) path

If the object is NSString, Nsdictionary, Nsarray, NSData, NSNumber, etc., you can archive it directly with Nskeyedarchiver.


If it is a custom model object, the model object needs to follow the Nscoding protocol and implement the method:

-(void) Encodewithcoder: (Nscoder *) encoder-(instancetype) Initwithcoder: (Nscoder *) decoder


Nscoder Encoder

Encodewithcoder: The method should encode the attributes to be archived, using the method beginning with encode.

Initwithcoder: The method should decode all the data in the archive (to the attribute), using the method beginning with decode.


Objective-c (10) Abstract Parent Class---protocol

Related Article

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.