Objective-c the difference between an instance variable and a property

Source: Internet
Author: User



  Remember that when you first learned OC, it was always unclear to instance variables and attributes.



For example, when printing the name of the person class object in the code below, when to use name with "_",NSLog (@ "%@",p1->_name);



When to use the name without "_",NSLog (@ "%@", p1.name);



Now in retrospect is also very interesting in the past, write down their own lost, remember the youth?






First, you write code using instance variables. As well as instance variable assignment to use Getter, setter method.


@interface Person: NSObject
{
   @public
   NSString * _name; // Define instance variable _name
}
-(instancetype) initWithName: (NSString *) name; // initialize
-(void) setName: (NSString *) name; // setter method
-(NSString *) name; // getter method
@end
@implementation Person // Implementation section
-(instancetype) initWithName: (NSString *) name {
     if (self = [super init]) {
         _name = name;
     }
     return self;
}
-(void) setName: (NSString *) name {
     _name = name;
}
-(NSString *) name {
     return _name;
}
@end
int main (int argc, const char * argv []) {// Main function
     Person * p1 = [[Person alloc] initWithName: @ "zhangsan"];
     NSLog (@ "% @", p1-> _ name);
     return 0;
}

The above code, all using the instance variable. We should all understand this. So when does it not underline?






Write code with attributes (compare the amount of code for both)



First clear two points:



1, the function of the property is to generate setter and getter method implementation. And if the corresponding instance variable is not defined in the. h file, an instance variable of "_ Property name" is generated, but the visibility of the generated instance variable is private.



2, the purpose of providing attributes in OC: In order to simplify the programmer's coding.


@
@interface Person: NSObject
@property (nonatomic, copy) NSString * name;
-(instancetype) initWithName: (NSString *) name;
@end

@implementation Person
-(instancetype) initWithName: (NSString *) name {
     if (self = [super init]) {
         _name = name;
     }
     return self;
}
@end

int main (int argc, const char * argv []) {
     Person * p1 = [[Person alloc] initWithName: @ "zhangsan"];
     NSLog (@ "% @", p1.name); // Properties are a pair of getter and getter methods, and dot syntax is another format for calling attributes.
return 0;
}

  At this point, although the property also generates the corresponding instance variable, the instance variable is private, and all the instance variables cannot be printed directly. Using the DOT syntax, the equivalent of calling the Getter method, the return value is still the value stored by the instance variable.



Because the keyword that defines the attribute when the semantic setting is copy.



Setter Internal Implementation


 
 - (void)setGender:(NSString *)gender {  if (_gender != gender) { 
 [_gender release]; 
 _gender = [gender copy];
   } 

}



Getter Internal implementation


-(NSString *) gender { return  [[autorelease];  


}



  In this case, you should be able to understand the difference between an attribute and an instance variable.






Objective-c the difference between an instance variable and a property


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.