Summary of black marathon _ video learning <objective-C> ---- 03 self, nsstring, @ property, @ synthesize, ID

Source: Internet
Author: User

---------------------- ASP. NET + unity development,. Net training, and hope to communicate with you! ----------------------

 

1. self1. self indicates who calls the current method. Two cases: Self appears in the object method, and Self represents the object; self appears in the class method, and Self represents the class. 2. The object method uses "Self → member variable name" to access the member variables in the current object. 3. [self method name] can call other object methods and class methods.

 

II. Introduction to nsstring1.nsstring: nsstring is a string processing class. The OC string is also an object. For example, @ "sfhgj" is also an object, and its class is nsstring, which is in the foundation framework. 2. Length of the nsstring class method, which can return the number of characters in the string.

 

3. @ property and @ synthesize

1. @ property @ synthesize keyword

Note: These two keywords are compiler features, allowing xcode to automatically generate the getter and setter declarations and implementations.

(1) @ property keyword

@ Property Keyword: the setter and getter methods of a member variable can be automatically generated.

@ Property int age;

This line is automatically extended into the following two sentences during compilation:

- (void)setAge:(int)age;- (int)age;

(2) @ synthesize keyword

The @ synthesize keyword is used to generate the setter and getter methods of member variables.

Syntax: @ synthesize age = _ age;

It is equivalent to the following code:

- (void)setAge:(int)age{_age=age;}- (int)age{Return _age;}

(3) usage and usage of keywords

Class declaration:

Class implementation:

Main function:

New Version:

Class declaration:

Class implementation:

Main function:

 

(1) in old-fashioned code, @ property can only be written in @ interface @ end, and @ synthesize can only be written in @ implementation @ end. Since xcode 4.4, @ property exclusively covers the functions of @ property and @ synthesize.

(2) @ property int age; this sentence completes three functions: 1) generate the get and Set Methods declaration of the _ age member variable; 2) generate the _ age member variable set and get method; 3) generate a _ age member variable.

Note: The member variables generated in this way are private.

3) You can add int _ age in {}. The declared _ age displayed is protected.

(4) Principle: The get and set methods are the same as the variables. If you define them yourself, use the ones you have defined. If they are not defined, generate one automatically.

(5) manual implementation:

1) if the set method is manually implemented, the compiler will only generate the get method and member variables;

2) If you manually implement the get method, the compiler will only generate the Set Method and member variables;

3) if the set and get methods are both manually implemented, the compiler will not generate member variables.

Iv. ID

ID is a type of universal pointer that can point to \ to operate on any object.

Note: In the ID definition, the * number is already included. The ID pointer can only point to the OS object.

ID type definition

Typedef struct objc object{Class isa;} *id;

Limitation: the compiler will immediately report an error when calling a non-existent method.

 

 

 

---------------------- ASP. NET + unity development,. Net training, and hope to communicate with you! ----------------------

For details, see www.itheima.com.

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.