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.
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.)
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With