OC for iOS Development (eight)--supplement to the Little Knowledge supplement Init method

Source: Internet
Author: User

Small Knowledge Supplement

First, ID

1. Introduction

A universal pointer that can point to any OC object, equivalent to NSObject *

Definition of ID Type

struct Objc_object {        *ID;

2. Use

Note: Do not add * after ID

ID p = [person new];

3. Limitations

Call a non-existent method, the compiler will immediately error

Ii. extraction of files of. h and. m

1) Each class is distributed in different files

2) The declaration of the class is placed in the. h file, the implementation of the class is placed in the. m file

3) If you want to use a class, include the. h declaration file for a class

iii. addition of the Init method (emphasis)

1. Principle of object creation

New Split two-part song

U Allocate Memory (+ALLOC)

U initialization (-init)

Person *P1 =*p1 =*p = [[Person alloc] init];

2. Overriding the Init method

L WANT the member variable to have some default values right after the object is created

L Rewrite process of init method

-(ID) init{
1. Be sure to call Super's Init method: Initialize some member variables and other properties declared in the parent class
Self = [super init]; Current Object Self
2. If the initialization succeeds, it is necessary to proceed with the next initialization if (self = [super init]) {//Initialize success ; } return Self ;//3. Returns a value that has already been initialized}

3. Custom Construction methods

Some specifications of the L-structure method

    • -(ID) initwithage: (int) Age {    if "self = [super init]")        {= age ;    }     return Self ;}

      Pass multiple parameters for initialization

      -(ID) initwithage: (int) Age Andno: (int) No;

      The return value is the ID type

    • Method names start with Init

OC for iOS Development (eight)--supplement to the Little Knowledge supplement Init 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.