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:
- @interface MyClass : NSObject {
- NSString *name;
- }
- @end
- @implementation MyClass//something...
- - (void)dealloc{
- [name release];
- [super dealloc];
- }
- @end
What if name is an attribute variable? In the Basic IPhone development tutorial, you will often see the following code:
- @interface MyClass : NSObject {
- NSString *name;
- }
- @property(retain) NSString *name;
- @end
- @implementation MyClass @synthesize name;
- - (void)dealloc{ self.setName = nil;
- [super dealloc];
- }
- @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:
- - (void) setName:(NSString *)
- value {
- [value retain];
- // calls [nil retain], which does nothing [name release];
- // releases the backing variable (ivar) name = value;
- // 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.
- http://stackoverflow.com/questions/192721/why-shouldnt-i-use-objective-c-2-0-accessors-in-init-dealloc
-
- 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!