Dark Horse programmer--objective-c--@property and @synthesize keywords

Source: Internet
Author: User



The property is a objective-c keyword, used in pairing with @synthesize , to allow the compiler to automatically generate a method declaration with the same name as the data member. @synthesize is the implementation that is used to generate the corresponding declaration method.


One, @property keywords1, the property of the syntax format:


@property (parameter 1, Parameter 2) type name;



Here are the main parameters, the following three kinds:



(1) Setter/getter method (assign/retain/copy)



(2) Read-write attributes (readwrite/readonly)



(3) atomicity (nonatomic)



2, three ways to use



(1) Assign/retain/copy represents the way to assign value



(2) the ReadOnly keyword means that the setter will not be generated, so it cannot be used in combination with Copy/retain/assign .



(3) the default value for Atomicity is Atomic, and the Read function is atomic.



Copy/reain/assign selects a setter in which to determine the properties of the property to handle this property. The NSObject object is used in this way.



Some special object such as Nssstring uses copy.



The Assign keyword represents the setter's direct assignment, rather than copying or preserving it. For basic data types, such as Nsinteger and cgfloat, or types that you do not directly own, such as delegates.



3. How to use @property  



/* Member variables are not defined using the @property case */

#import <UIKit/UIKit.h> @interface  viewcontroller:uiviewcontroller  { *_obj; 

-(void) viewdidload;

@end

@implementation Viewcontroller

-(void) viewdidload  {      [super viewdidload];          Self.obj = Nil; 



At this point the compiler will error.







/ * Define member variables using @ property * /

#import <UIKit / UIKit.h>
  
@interface ViewController: UIViewController
{
     NSObject * _obj;
}
@property (nonatomic, retain) NSObject * obj;
@end 





Compile can pass, run, crash, prompt error reason: '-[viewcontroller setobj:]: Unrecognized selector sent to instance 0x6b6c480



That's all we have. Implements setter methods.



Use the @synthesize keyword to implement getter and setter.


  @implementation ViewController @synthesize obj; - (void)viewDidLoad  
    {  
        [super viewDidLoad];  
        self.obj = nil;  
    } 





Run, the program runs normally. The setter worked.



4. Code generated by @property and @synthesize keywords


 
 
#import <UIKit/UIKit.h>  
  
@interface ViewController : UIViewController  
{  
    NSObject *obj;  
}  
//@property (nonatomic,retain) NSObject *obj;  
-(NSObject*)obj;  

-(void)setObj:(NSObject*)newObj;  
@end 




  @implementation ViewController  
    //@synthesize obj;  
    - (void)viewDidLoad  
    {  
        [super viewDidLoad];  
        self.obj = nil;  
    }  
      
    -(NSObject*)obj{  
        return obj;  
    }  
    -(void)setObj:(NSObject*)newObj{  
        if(obj != newObj){  
            [obj release];  
            obj = [newObj retain];  
        }  
    }  





Run again, it can start normally. A description of the getter and setter he wrote replaced the property.



5, using three kinds of parameters comparison


@property (nonatomic,retain)NSObject *obj;

@property (nonatomic,retain,readwrite) NSObject *obj;





ReadWrite is the default behavior, so these two lines of code are equivalent




@property (retain) NSObject *obj;

@property (atomic,retain) NSObject *obj;





Atomic is the default behavior, so these two lines of code are equivalent.




@property (retain) NSObject *obj;

@property (atomic,retain) NSObject *obj;





for int, atomic assign is the default behavior, so these three rows are equivalent.





@property(atomic,assign)int number;        

@property(atomic) int number;        

@property int number;





Is that OK? No, it's a warning.












Only the underlying data types such as int can be written like this. The object must be assigned the type of the value.




@property  (retain) NSObject *obj;





That's fine. When to use assign, when to use retain, and later to speak.



Second, @synthesize key words


//
// Person.m
// 25_Property
//
// Created by jiangwei on 14-10-12.
// Copyright (c) 2014 jiangwei. All rights reserved.
//

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

// Sometimes we don't want to define the attribute as _
// At this time we can use @synthesize to modify the property name we want

// At this time the attribute _userName becomes $ userName

@implementation User
@synthesize userName = $ userName;

@end 





Since we use @property to define properties, if we want to modify the name of this property, we can use the @synthesize keyword to modify the property name





@synthesize userName = $userName;





Dark Horse programmer--objective-c [email protected] and @synthesize keywords


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.