Objective-c Response method and construction method

Source: Internet
Author: User

1. Response method

Bool isinstace = [P respondstosesector: @selector (run)];

No parameters

-(ID) performselector:selector (apply selector specified method) NSObject method

A parameter

-(ID) performselector:selector withobject:object (apply selector specified method, pass parameter object)

Two parameters

-(ID) performselector:selector withobject:object1 withobject:object2 (apply selector specified method, pass parameters Object1 and Object2)

2. Construction method

Create object: Person *p=[person new];

3 things are done inside the new method:

(1) Use the Alloc method to allocate storage space (returns the allocated object);

(2) Initialize the object using the Init method.

(3) Returns the first memory address of the object.

The new method can be opened as follows:

(1). Call class method +alloc allocate storage space, return uninitialized object

Person *p1=[person alloc];

(2). Call object method-init to initialize, return the object itself

Person *P2=[P1 Init];

(3). The above two processes are integrated into one sentence:

Person *p=[[person alloc] init];

(1) The Init method is the constructor method, which is the method used to initialize the object, called the construction initialization.

(2) Alloc

Sends the result of a alloc message to a class that allocates memory for the class (the object's memory address already has) to hold all the instance variables of the class.

Attention

An object that has just been allocated cannot be used immediately.

The object needs to be initialized before it can be used.

However, due to uninitialized behavior, some unpredictable behaviors can occur later.

Overriding construction methods:

1. The role of [Super Init]: The initialization method of the parent class is called first, and the member variables inherited from the parent class are initialized. When the initialization is finished, return my current object pointer.

2. Overriding Construction method Considerations:

Construction method Use note

(1) A subclass has member variables that include its own member variables and member variables inherited from the parent class, and the member variables that inherit from the parent class should be initialized first when overriding the constructor method.

(2) Principle: Initialize the parent class first, and then initialize the subclass.

(3) The purpose of overriding the construction method: In order for the object method to be created, the member variable will have some fixed value.

(4) Note: First call the constructor method of the parent class [Super Init]; The initialization of the member variables within the subclass.

3. Construction Method Application Scenario:

If some of the properties in an object need to have values when they are initialized, they can be done using a construction method.

Custom Construction Methods

1. Specification of custom construction methods

(1) must be an object method, starting with a minus sign

(2) The return value is typically an ID type or a instancetype type

(3) method names usually start with Initwith

2. Implementation of custom construction methods

-(Instancetype) init

{

self = [super init];

if (self) {

The member variable to initialize.

}

return self;

}

3. Use of custom construction methods note

(1). Do your Own thing

(2). The method of the parent class is given to the parent class's method to handle, and the subclass's method handles the subclass's own unique properties

Custom Construction Methods

1. Specification of custom construction methods

(1) must be an object method, starting with a minus sign

(2) The return value is typically an ID type or a instancetype type

(3) method names usually start with Initwith

2. Implementation of custom construction methods

-(Instancetype) Initwithname: (NSString *) name andage: (int) age;

{

self = [super init];

if (self) {

The member variable to initialize.

_name = name;

_age = age;

}

return self;

}

3. Use of custom construction methods note

(1). Do your Own thing

(2). The method of the parent class is given to the parent class's method to handle, and the subclass's method handles the subclass's own unique properties

4. Usage Scenarios

If an object is created, it is necessary to initialize (set) Some of the properties in it, this time you can use the constructor method

Objective-c Response method and construction method

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.