Key Issues re Objective-C Property Syntax

I have a few basic questions regarding the syntax and use of properties in Objective-C:

Assume the following declaration in the title:

@interface TestObject : NSObject { NSArray *myArray; } @property (nonatomic, retain) NSArray *myArray; 

In the implementation, I can:

  • List item
  • Use myArray and self.myArray interchangeably for setting and getting goals?
  • Is self.myArray = nil equivalent to [myArray release] ?
    If so, is there a reason to use self.myArray = nil rather than [myArray release] ?
+4
source share
3 answers
  • myArray and self.myArray are actually different. myArray accesses the variable directly, while self.myArray (equivalent to [self myArray] ) calls the accessor. Most people agree that you should use self.myArray (or [self myArray] ) all the time and never use myArray directly. This is due to the fact that the accessory may have side effects; for example, KVO will not work if you set your variable directly and memory management will not be processed for you.

  • Your property is declared using retain , so self.myArray = anArray (the same as [self setMyArray:anArray] ) does the following:

    • Save anArray, which will soon become the new myArray.
    • Release the old myArray, which soon will no longer be myArray.
    • Change the myArray pointer to point to anArray now.

So when you do self.myArray = nil , one of the steps (# 2) really frees the old array. (And since the new one is nil , we need not worry about its memory management, although we saved it.) So, yes, self.myArray = nil is a valid way to free myArray .

HOWEVER, if you are talking about releasing myArray in dealloc , it is generally recommended that you use [myArray release] , because calling self.myArray = nil will have side effects if any other objects observe myArray through QUO. Therefore, when it executes following the memory management standards, it is not recommended to write your dealloc method using self.myArray = nil .

+5
source

Both existing answers are incorrect.

@synthesize creates setters and getters that look like this:

 - (void)setMyArray:(NSArray*)array { if( myArray != array ) { [myArray release]; myArray = [array retain]; } } - (NSArray*)myArray { return myArray; } 

(Note that they are not exactly the same, and differ if you specify a copy or other attributes, but this is the basic formula). Now we can see that self.myArray = nil; will release the old array. self.myArray and myArray are not interchangeable for customization purposes. Moreover, self.myArray = nil; will continue to work in the world of garbage collection.

As Dave Delong points out, self.myArray = nil will notify any observer myArray of the changed value, which can be a problem if you do this in your dealloc method. To avoid this case, your dealloc will look something like this:

 - (void)dealloc { [myArray release]; myArray = nil; [super dealloc]; } 

(note myArray = nil; is a stylistic choice here.)

+4
source

To configure setters / getters, you need to implement them mainly as:

 + (NSArray*) myArray { return myArray; } + (void) setMyArray:(NSArray*)input{ myArray = input; } 
-3
source

Source: https://habr.com/ru/post/1303832/


All Articles