OC basics -- Property, oc -- property

Source: Internet
Author: User

OC basics -- Property, oc -- property

Compiler commands:

Used to tell the compiler what to do

@ Property:

@ Property is the instruction of the compiler, which tells the compiler to automatically generate the setter and getter declarations in @ interface.

@ Synthesize:

@ Synthesize is the compiler instruction that tells the compiler to automatically generate setter and getter implementations in @ implementation.

Manually write setter-getter:

#import <Foundation/Foundation.h>
@interface Member : NSObject{ @public NSString * _name; int _age; NSString * _account; NSString * _password;}- (void) setName: (NSString *) name;- (NSString *) name;- (void) setAge: (int) age;- (int) age;- (void) setAccount: (NSString *)account;- (NSString *) account;- (void) setPassword: (NSString *) password;- (NSString *) password; @end #import "Member.h" @implementation Member- (void) setName: (NSString *) name{ _name = name;}- (NSString *) name{ return _name;}- (void) setAge: (int) age{ _age = age;}- (int) age{ return _age;}- (void) setAccount: (NSString *)account{ _account = account;}- (NSString *) account{ return _account;}- (void) setPassword: (NSString *) password{ _password = password;}- (NSString *) password{ return _password;}@end

Use @ property and @ synthesize:

#import <Foundation/Foundation.h>@interface Member : NSObject{@public    NSString * _name;    int _age;    NSString * _account;    NSString * _password;}@property NSString * name;@property int age;@property NSString * account;@property NSString * password; @end#import "Member.h" @implementation Member
@synthesize name = _name;@synthesize age = _age;@synthesize account = _account;@synthesize password = _password;
@end

@ Property and @ synthesize description:

@ Property:

As long as the compiler sees @ property, it knows that we want to generate the declaration of the getter/setter Method for a certain attribute.

/*-(Void) setAge :( int) age;-(int) age; * // use @ property to equivalent the above two sentences @ property int age;

When using @ property for declaration, do not need to underline _

// The underline is equivalent to the following two sentences: @ property int _ age;/*-(void) set_age :( int) _ age;-(int) _ age ;*/

@ Synthesize:

After @ synthesize, tell the Compiler which @ property declaration should be implemented

/*-(Void) setAge :( int) age {_ age = age;}-(int) age {return _ age ;} * // use @ synthesize to equivalent the above part. // if the member Variable _ age does not exist, a private member Variable _ age is automatically generated. m implementation file) @ synthesize age = _ age;

Tell @ synthesize to assign the value to and returned to the caller.

/*-(Void) setAge :( int) age {_ number = age;}-(int) age {return _ number ;} * /// if the read and write operations in this way are _ number instead of _ age @ synthesize age = _ number;

If no value is assigned to the user after @ synthesize, the system assigns a value to the member variable with the same name as @ synthesize by default.

# Import <Foundation/Foundation. h> @ interface Person: NSObject {@ public int _ age; int age ;}@ property int age; @ end # import "Person. h "@ implementation Person @ synthesize age; @ end # import <Foundation/Foundation. h> # import "Person. h "int main (int argc, const char * argv []) {Person * p = [Person new]; [p setAge: 88]; NSLog (@ "_ age = % I, age = % I", p-> _ age, p-> age); return 0 ;}/ * output result: 19:13:45. 846 synthesize basically uses [813: 21032] _ age = 0, age = 88 so @ synthesize age; read and write is the age attribute instead of the _ age. If the member variable age does not exist, A private member variable age (in. m implementation file )*/

You can set multiple attributes by @ synthesize, and use commas to connect multiple attributes.

      @synthesize name = _name, age = _age, account = _account, password = _password;

Private member:

Access modifier:

@ Public

> You can access the publicly modified member variables in other classes.

> You can also access the public modified member variables in this class.

> You can access the member variables modified by the public in the parent class in the subclass.

@ Private

> Private modified member variables cannot be accessed in other classes.

> You can access the private modified member variables in this class.

> You cannot access the member variables modified by private in the parent class in the subclass.

@ Protected

> Member variables modified by protected cannot be accessed in other classes.

> You can access the member variables modified by protected in this class.

> You can access the member variables modified by protected in the parent class in the subclass.

Note: by default, all instance variables are protected.

@ Package

> Between public and private

If it is accessed in other packages, it is private.

If the access is public in the package of the current Code

Private variable:

The member variables written in @ implementation are private member variables by default. The member variables defined in @ implementation are different from those modified by @ private. They cannot be viewed in other classes, this private variable can only be accessed in this class.

Variables defined in @ interface, No matter what member variable modifier (including @ private) is used ), we can all see this variable in other classes, but we can't operate the variable that has the modifier.

Private method:

If there is only method implementation and there is no method declaration, this method is a private method, but there is no real private method in OC, because OC is a message mechanism.

// Only the private method id pp = [Person new]; [pp test]; Person * p = [Person new]; [p performSelector: @ selector (test)];

@ Property Enhancement

1. After Xcode4.4, apple has enhanced @ property. In the future, only one @ property can be used to generate the description and Implementation of the setter/getter method at the same time.

2 by default, @ property assigns the passed attribute to the member variable starting _.

3. If the @ property method is used to generate the getter/setter method, the system will automatically generate a member variable starting _.

Note: @ property automatically generates a member variable, which is generated in the. m file rather than in the. h file.

4 @ property has one drawback: It only generates the declaration and implementation of the simplest getter/setter method, and does not filter incoming data, to filter incoming data, we must override the getter/setter method.

If you override the setter method, the property will only generate the getter Method

If you override the getter method, the property will only generate the setter method.

If the getter/setter method is rewritten at the same time, the property will not automatically generate private member variables for us.

#import <Foundation/Foundation.h>@interface Member : NSObject @property NSString * name;@property int age;@property NSString * account;@property NSString * password;@end#import "Member.h"@implementation Member@end

 

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.