Objective-c language-point syntax and variable scope-@property @synthesize and ID

Source: Internet
Author: User
Tags variable scope





One, dot syntax



(i) Cognitive point grammar



Declare a person class:


#import <Foundation / Foundation.h>

@interface Person: NSObject
{
     int _age; // default is @protected
}

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

@end 
View Code





Implementation of the person class:





#import "Person.h"

@implementation Person

-(void) setAge: (int) age
{
     _age = age; // cannot be written as self.age = newAge, quite similar to [self setAge: newAge];
}

-(int) age // get method
{
     return _age;
}

@end 
View Code








Use of point syntax:


#import <Foundation / Foundation.h>
#import "Person.h"

int main (int argc, const char * argv [])
{

     @autoreleasepool {
        
         // insert code here ...
         Person * person = [[Person alloc] init];
        
         // [person setAge: 10];
         person.age = 10; // dot syntax, equivalent to [person setAge: 10];
// This is not to assign a value to the person's property, but to call the person's setAge method
        
         // int age = [person age];
         int age = person.age; // equivalent to int age = [person age]
        NSLog (@ "age is% i", age);
         [person release];
        
     }
     return 0;
} 
View Code








(ii) The role of Dot grammar



OC Design Point syntax is designed to enable developers of other languages to quickly get started with OC language development, using DOT syntax, which is similar to other object-oriented languages like Java.



(c) The nature of Point grammar



The essence of point syntax is the invocation of the method, not the access to the member variable, and the compiler automatically expands to the appropriate method when the point syntax is used. Remember that the essence of point syntax is to convert to the appropriate set and get methods, and you cannot use point syntax without the set and get methods.



Such as:



stu.age=10; unfold as: [Stu setage:10];



int a=stu.age; expand to: [Stu Age];



How does the compiler know if it is a set method or a Get method? The main point is to look at the assignment (you can use breakpoint debugging to see it).



There is only one way to access member variables in OC, such as Stu->age, which is required in the case of @public.



(d) Use of point grammar note



The following usage is a dead loop:



(1) In the Set method, Self.age=age; equivalent to [self setage:age];



(2) in the Get method, return self.age;



Ii. Scope of variables



(a) The scope of the variable is divided into four main types:



(1) @public (public) can be accessed directly from anywhere with an object.



(2) @protected (protected) can only be accessed in the object methods of the current class and subclass



(3) @private (private) can only be accessed directly in the object method of the current class



(4) @package (frame-level) scopes are between private and public, and can be accessed directly from the variable name as long as they are in the same frame



(ii) Use of attention and supplement



(1) member variables can also be declared in the implementation of the class, the. m file, but because in other files it is usually just a header file that does not contain an implementation file, the member variables declared here are @private. member variables defined in. m cannot drink its header file. The member variable in h has the same name, and it is futile to use keywords such as @public during this period.



(2) member variables declared between the @interface @end are protected by default if they are not specifically described.



(3) When one class inherits another, it has all the member variables and methods of the parent class, noting that all the member variables have it, except that it cannot be accessed directly.














Three, @property @synthesize keywords



Note: These two keywords are compiler features that allow Xcode to automatically generate the declarations and implementations of getter and setter.



(i) @property keywords



@property keyword can automatically generate a declaration of the setter and getter method for a member variable



@property int age;



When the compilation encounters this line, it is automatically extended to the following two sentences:



-(void) Setage: (int) age;



-(int) age;






(ii) @synthesize keywords



@synthesize keyword helps generate the implementation of the setter and getter methods for member variables.



Syntax: @synthesize age=_age;



Equivalent to the following code:



-(void) Setage: (int) Age



{



_age=age;



}



-(int) age



{



Return _age;



}






(iii) Use and use of keywords note



The declarations section of the class:






The implementation part of the class:






Test procedure:






In the new version:



The declarations section of the class:






The implementation part of the class:






Test procedure:






(1) in the old code, @property can only be written in the @interface @end, @synthesize can only be written in the @implementation @end, since Xcode 4.4, @ The property is the function of @property and @synthesize.



(2) @property int age; 3 functions: 1) generate declarations of get and set methods for _age member variables, 2) Generate _age member variable set and get method implementations, 3) generate a _age member variable.



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



(3) You can add int _age to {}, and the declared _age is protected.



(4) Principle: Get and Set methods are the same as variables, if you define yourself, then use what you have defined, if not defined, then automatically generate a.



(5) Manual implementation:



1) If the Set method is implemented manually, then the compiler generates only the get methods and member variables;



2) If the Get method is implemented manually, then the compiler generates only the set method and member variable;



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






Iv.. Id



An ID is a type, universal pointer that can point to \ manipulate any object.



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



Definition of ID Type



Typedef struct OBJC object{



Class Isa;



} *id;



Limitations: Call a nonexistent method, the compiler will immediately error.






Objective-c language-point syntax and variable scope [email protected] @synthesize and ID


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.