How to Release property variables of iPhone Development

Source: Internet
Author: User

IPhone DevelopmentHow are the attribute variables?ReleaseIs the content to be introduced in this article, mainly to release the instance variables owned by the object, the common method is to call in deallocReleaseFor example, the following code:

 
 
  1. @interface MyClass : NSObject {    
  2. NSString *name;  
  3. }    
  4. @end   
  5. @implementation MyClass//something...  
  6. - (void)dealloc{    
  7. [name release];    
  8. [super dealloc];  
  9. }   
  10. @end 

What if name is an attribute variable? In the Basic IPhone development tutorial, you will often see the following code:

 
 
  1. @interface MyClass : NSObject {    
  2. NSString *name;  
  3. }  
  4.  @property(retain) NSString *name;   
  5.  @end   
  6.  @implementation MyClass @synthesize name;  
  7.   - (void)dealloc{  self.setName = nil;    
  8.   [super dealloc];  
  9.   }   
  10. @end 

Instead of directly accessing the variable itself, the setter automatically generated by the compiler is used. That's the problem. What should I do if I assign a value of nil to release? Think about how the General setter writes it. See the following:

 
 
  1. - (void) setName:(NSString *)   
  2. value {     
  3. [value retain];   
  4. // calls [nil retain], which does nothing   [name release];   
  5. // releases the backing variable (ivar)   name = value;     
  6. // sets the backing variable (ivar) to nil} 

OK. But isn't that true? Please refer to the discussion below, there will be problems in the KVC mechanism.

 
 
  1. http://stackoverflow.com/questions/192721/why-shouldnt-i-use-objective-c-2-0-accessors-in-init-dealloc  
  2.  
  3. http://stackoverflow.com/questions/1283419/valid-use-of-accessors-in-init-and-dealloc-methods 

Summary:IPhone DevelopmentHow are the attribute variables?ReleaseI hope this article will help you!

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.