[Study note-objective-c] "objective-c-Basic Tutorial 2nd Edition" 13th Chapter Agreement

Source: Internet
Author: User
Tags shallow copy

13.1 protocol

Formal agreement: a list of names that contain methods and properties.

Attention:

    1. After adopting the protocol, the class will implement all the methods of the protocol.
    2. Typically, a protocol has only a few methods that need to be implemented.
    3. In the protocol, new instance variables are not referenced.
13.11 Declaration Agreement
    • Nscopying protocol
@protocol NSCopying- (id) copyWithZone: (NSZone *) zone;@end//如果采用了NSCopying协议,你的对象将会知道如何创建自身的副本
    • Inherit parent agreement
@protocol MySuperDuberProtocol <MyParentProtocol>@end//需要实现两个协议中的所有方法

Note: new instance variables are not introduced in the Protocol

    • Nscoding protocol
@protocol NSCoding//将对象的实例变量转换为NSCoding类的对象- (void) encodeWithCoder: (NSCoder *) aCoder;//从NSCoder类的对象中提取经过转换的实例变量,并使用它们去初始化新的对象- (id) initWithCoder: (NSCoder *) aDecoder;@end
13.12 Adoption Protocol
@interface Car : NSObject <NSCopying, NSCoding>{  // 该类的对象可以对自身进行编码解码;能够创建自身的副本}// methods@end// Car
13.2 Replication

Types of replication

    1. Shallow copy : Only a pointer to the referenced object is copied, the total number of objects is constant, and the number of pointers is.
    2. Deep Copy : Copies the referenced objects, the total number of objects.
13.21 Copying engine objects
    • Interface for engine class (no instance variable):
@interface Engine : NSObject <NSCopying>@end// Engine
    • Engine class declaration, using the Nscopying protocol, it is necessary to implement Conywtihzone: Method
@implementation- (id) copyWithZone: (NSZone *) zone{  Engine *engineCopy;  engineCopy = [[[self class] allocWithZone: zone] init];  return// copyWithZone

Attention:

    1. Zone is an object of the Nszone class that points to an area of memory that can be allocated.
    2. When you send a copy message to an object, the copy message is converted to Copywithzone before it reaches your code: method.
[selfclass];//获得self参数所属的类,当前类,非子类

Allocwithzone: Is the class method:

+ (id) allocWithZone: (NSZone *) zone;

allocWithZone:is a class method that allocates memory and creates a new object of that class, returning an object that holds a counter value of 1 and does not need to be disposed.
init: Initializes a new object

13.22 Copy Tire

Interface for the tire class (with two instance variables) (with the Nscopying protocol):

@interface Tire : NSObject <NSCopying>{  float pressure;  ... methods@end // Tire

Implementation copyWithZone: method

copyWithZone: (NSZone *) zone{  Tire *tireCopy;  tireCopy = [[[selfclass] allocWithZone: zone]                  initWithPressure: pressure                  treadDepth: treadDepth];  return (tireCopy);} // copyWithZone

Note: The specified initialization function for the tire class is used here, but it is not required. When setting properties is unlikely to involve additional work, we can also use the simple Init method to initialize, using accessor methods to modify the properties of the object.

13.23 Copying the Allweatherradial class (subclass of the tire Class)
@interface AllWeatherRadial : Tire- properties;- methods;@end

Note : This class is a subclass of the tire class, and the nscopying protocol is obtained.

- (id) copyWithZone: (NSZone *) zone{  AllWeatherRadial *tireCopy;  tireCopy = [super copyWithZone: zone];  [tireCopy setRainHandling: rainHandling];  [tireCopy setSnowHandling: snowHandling];  return// copyWithZone

This code explains:

This class just asks it super-class for a copy and hopes, the superclass does the right thing and uses [self class] When allocating the object. Because Tire ' s copywithzone:uses [self class] to determine the kind of object to make, it'll create a new Allweatherrad Ial.

13.24 Copying Car class
//Car:@interface Car : NSObject <NSCopying>{     NSMutableArray *tires;     ... methods@end // Car

Implementation copyWithZone: Method:

//Car.m- (id) copyWithZone: (NSZone *) zone{    Car *carCopy;    //self所属的类分配新对象    carCopy = [[[self class] allocWithZone: zone] init];    self.name;    Engine *engineCopy;    copy] autorelease];    carCopy.engine = engineCopy;    int i;    for04; i++)     {        Tire *tireCopy;        tireCopy = [[selfcopy];        [tireCopy autorelease];        [carCopy setTire: tireCopy atIndex: i];    }  return
13.25 parameter type: ID

Qualification parameters: The received object must be an accepted object

-(void)setObjectValue:(id<NSCopying>) object;
13.3 OBJECTIVE-C 2.0 new features
@protocol BaseballPlayer- (void//必须实现@optional- (void)slideHome;- (void)catchBall;- (void)throwBall;@required- (void//必须实现@end  // BaseballPlayer

Note :
The informal agreement in cocoa was gradually replaced by a formal agreement with the @optional approach.

13.4 Delegate Methods

Delegate: An object specifies the design pattern for another object to handle certain tasks, and the delegate adheres to the Protocol

The delegate method for the Nsnetservicebrowser class:

-(id<NSNetServiceBrowserDelegate>)delegate;//返回当前的委托对象-(void)setDelegate:(id<NSNetServiceBrowserDelegate>)delegate;//设置委托:只有遵守该协议的对象才能被设置为委托

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Study note-objective-c] "objective-c-Basic Tutorial 2nd Edition" 13th Chapter Agreement

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.