Bobby
Bobby

Reputation: 537

Memory Management for iOS

So I have recently just starting to look into memory management a little more in iOS but I am completely confused right now (probably overthinking it)...

For example, if you have an object A (a ViewController) strongly holding onto object B (An NSArray declared as strong in the header file of the view controller); When you specify the property of the NSArray to be strong does that mean the ViewController has a pointer to the NSArray? If that is true, what I don't understand is where are you explicitly saying that the view controller is a pointer to the NSArray object? Or does the "strong" keyword imply that the view controller has a pointer to the memory location where the NSArray is stored. Like for example, we state that the variable myArray is a pointer to an object of type NSArray in this line of code: @property(strong, nonatomic) NSArray *myArray; Is something like this done behind the scene for the view controller?

Upvotes: 0

Views: 75

Answers (1)

Felipe Cypriano
Felipe Cypriano

Reputation: 2737

Think of a @property as the compiler automatically synthesizing these methods and instance variables for you:

@interface MyController {
    @private
    NSArray *_array;
}

- (NSArray *)array;
- (void)setArray:(NSArray *)array;

And the implementation of the setter is like this:

- (void)setArray:(NSArray *)array {
    if (array != _array) {
        _array = array; // strong means that ARC will retain the array here
    }
}

So your controller has (not is) a variable (_array) that points to an array.

Upvotes: 1

Related Questions