Property in the Objective-c

Source: Internet
Author: User



Where the property can be declared



Property can be declared in the Class Interface section and class extension and protocol






The visibility of the property



The property in Objective-c is actually the getter and setter method. The property of a class can be accessed externally, primarily to see if the property declaration of the class can be externally visible. For example, there is a class x whose. h and. m files are as follows:


 
 
//X.h

#import <Foundation/Foundation.h>

@interface X : NSObject

@end
//X.m

#import "X.h"

@interface X ()

@property (assign, nonatomic) int i;

@end

@implementation X



@end



If you access the property declared in X.M in the main function, you will get an error:


//main.m

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


int main (int argc, const char * argv []) {
     @autoreleasepool {
        
         X * x = [X new];
         x.i = 2; // error, property i is not visible
        
     }
     return 0;
} 


However, if we move the class extension in the East X.h, then the main function can see the declaration of property I, will not error. Alternatively, you can declare the setter method of property I in the interface section of X.h, because the main function accesses the actual setter method of property I, and after declaration, the setter method is visible to the main function. , so there is no error.






Name of property Composition instance variable



There are 3 cases in the property composition instance variable:



1) Do not use the @synthesize instruction, at this time the composite instance variable name is underlined with the property, that is _property



2) using only the @synthesize property name, the instance variable of the composition is the same as the property name at this time



3) Use @synthesize property name = Instance name, then the instance name of the composition is the specified name






When does the instance variable not be synthesized for the property



If you customize the Getter,setter method for a readwriteproperty or customize the Getter method for a ReadOnly property, the compiler does not automatically synthesize the corresponding instance variable. Unless displayed using @synthesize property name or @synthesize property name = Instance Name






Accessibility of an instance variable of a composition



The instance variables of the composition are private.






Default attribute for property



The default attribute for the property is strong (for reference types), Atomic,readwrite






Property in the Objective-c


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.