Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C ARC and Instance Variables iOS SDK

Are we supposed to convert all of our instance variables we wish to retain to private properties or am I missing something obvious?

@interface SomethingElse : Something  {

    NSMutableArray *someArray;

}

In this example, someArray is initialized in a method with [NSMutableArray initWithObject:someObject] but isn't retained.

My particular situation is I'm updating a game, there are a lot of instance variables, so I want to make sure I'm doing this right for the sake of future versions of the sdk.

like image 798
Bill Kervaski Avatar asked Jul 03 '12 15:07

Bill Kervaski


2 Answers

Are we supposed to convert all of the local variables we wish to retain to private properties or am I missing something obvious?

First, the variable you showed in your example is an instance variable, not a local variable. Local variables are declared inside a block of code (e.g. inside a function or method, or inside some sub-block such as the body of a conditional statement) and have a lifetime that's limited to the execution of the block in which they're declared. Instance variables are declared in a class; each instance of that class gets its own copy of the instance variables declared by the class.

Second, no, you don't need to convert all your instance variables to properties. Instance variables are treated as strong references by default under ARC. A property is really just a promise that a class provides certain accessors with certain semantics. Just having an instance variable doesn't mean that you have to provide accessors for that ivar. (Some might say that you should, but you don't have to.)

like image 86
Caleb Avatar answered Nov 02 '22 06:11

Caleb


A @property is the same as an instance variable unless you use other than the default storage modifier, which is strong for objects. Example, if you want @property (copy) NSString *s; either use an instance variable and remember to call copy each time you set the variable, or use the @property (which is easier).

like image 3
Jano Avatar answered Nov 02 '22 04:11

Jano