"OC Madness Handout" notes (v)

Source: Internet
Author: User

1, the point syntax in OC

Point syntax: Object name. Member variable name (point syntax is not an Access member variable, but a call to a method)
It is a compiler feature

Point syntax function: can replace the traditional get and set methods of the call

The premise of point syntax:

First you have to have get and set methods

Point Syntax Essence:

Call the Get and set methods

If the point syntax appears to the left of =, indicating that the set value is equivalent to calling the Set method

P.age = 10;

To be replaced by: [P setage:10];

If the point syntax appears to the right of =, it means that getting the value is equivalent to calling the Get method

int age = P.age;

To be replaced by: int = [P age];

To be replaced by: [P dispage:p.age];

[P dispage:[p age]];

Attention:

The point syntax essentially calls the get and set methods, so if you don't have a get and set method that defines a member variable, you can't use dot syntax at this time.


2, @property use, enhance

1) How to use the @property before Xcode4.4

(1) Just use @property as a way of declaring '

Format: @property int age; means to help us generate a get and set age
{
int age;
int _age;
NSString *_name;
}

@property int age;
-(void) Setage: (int) age;
-(int) age;

Manually implement get and set methods
-(void) Setage: (int) age{
Self->age = age;
}
-(int) age{
return age;
}


@property int _age;
-(void) Set_age: (int) _age;
-(int) _age;


@property NSString *name;
-(void) SetName: (nsstring*) name;
-(nsstring*) name;


(2) @synthesize keywords

@synthesize variable name;

Automatically help us implement the corresponding get and set methods

@synthesize Age,_age;

@synthesize the equivalent of helping us achieve the following method
-(void) Setage: (int) age{
Self->age = age;
}
-(int) age{
return age;
}

@synthesize name;

(3) @synthesize name = _b;

{
NSString *name;
NSString *_b;
}

@property NSString *name;
@synthesize name; The Get and set methods set the value of name

@synthesize name = _b; Value equivalent to Operation _b

-(void) SetName: (NSString *) name{
_b = name;
}

-(NSString *) name{
return _b;
}


Note: Before 4.4, @property and @synthesize appear in pairs

2) @property after Xcode4.4 (@property enhancement)

Enhanced use:
@property int score;

1). h file to help us declare the SetScore (setter) and score (getter) methods

2). m help us to generate a private variable for _score (cannot be accessed externally and cannot be inherited by the quilt Class)

3). Help us implement get and set methods in M


3, the use of @synthesize

4. Dynamic type &id type

Dynamic type: When the program is running, it can determine what type the object belongs to
Static type: When the program compiles, it determines the type of the object

The embodiment of the dynamic type: polymorphic, the parent pointer can point to the child class object

NSObject is the base class for all classes

NSObject *obj = [Dog new];
obj = [person new];

The parent class invokes a method specific to the subclass

[(person *) obj eat];

The ID type is also known as a universal pointer.

ID obj; Run-time Check
obj = [Car new];
[(car*) obj stop];


5. Dynamic Type detection method

7 methods

1) The judgment of the object's affiliation

Object is an instance object of the specified class or subclass

Iskindofclass

Object is an instance object of the specified class

Ismemberofclass

Class is a subclass of another class

Issubclassofclass

2) Determine whether the response method

Whether the object responds to the method

Class is capable of responding to methods

Class-To-response method


3) Response method

Pass parameters


6, the use of construction methods

[Person new];

--[Person alloc]; After the space application is completed, it can not be used immediately, it may lead to unexpected errors

[[Person alloc] init];

[Person new]; Will put the value of our member variable, the default setting is 0

Init is a construction method of our OC class
The constructor method is when the class is created, and the default invocation of the

Init is inherited from NSObject.

Specifies the member variable initialization value in the subclass by overriding the Init method of the parent class

The Init method has a fixed format:

-(ID) init{

Initialization of the parent class.
Determine if initialization is successful
if (self = [super init]) {


Initialize your own information
_age = 10;
}
Return
return self;
}

7. Custom Construction method


If you want to specify the initialization of a member variable value, you need to customize the constructor method

1) must be an object method

2) The return value is typically an ID type

3) must start with Initwith


-(ID) Initwithname: (NSString *) name{

Determine if initialization is successful
if (self = [super init]) {

Initialize your own information
_name = name;
}
Return
return self;


}

"OC Madness Handout" notes (v)

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.