emenegro
emenegro

Reputation: 6971

Instance variables, properties and transition to ARC

Finally I'm transitioning to ARC. Sounds too late but all my projects have retrocompatiilty to 3.0 (any news about App Store unsupporting?) so I can't use it. But now I'm working in a new project with base deployment in iOS 5 so I'm using ARC.

My question is so simple. I'm used to declare private instance variables and public properties. For example:

@interface MyClass : NSObject {
@private

    Var *aVar_;
    Var *anotherVar_;
}

@property (nonatomic, readonly) Var *aVar;

@end

@implementation MyClass

@synthesize aVar = aVar_;

@end

Inside the class I work with instance variables, not properties.

But now I'm trying to avoid instance variables because I think there are not neccessary and redundant if I use proeprties, and I read time ago that is better to use properties instead of instance variable, but I'm not sure. That class now seems like that

@interface MyClass : NSObject

@property (nonatomic, readwrite, strong) Var *aVar;

@end

@interface MyClass()

@property (nonatomic, readwrite, strong) Var *anotherVar;

@end

@implementation MyClass

@synthesize aVar = aVar_;
@synthesize anotherVar = anotherVar_;

@end

In this case I'm still using instance variables (underscored) to manage my data because is less verbose and ARC takes into account all memory issues, but I don't know if that is correct.

Also I have another question. The property of aVar in the first chunk of code is readonly but if I use only properties I have to make that property readwrite. If I want to make the public property readonly, do I have to declare a public readonly property in the @interface and a private readwrite in private @interface?

Thank you so much.

Upvotes: 0

Views: 755

Answers (3)

David Hoerl
David Hoerl

Reputation: 41632

The answer to your questions is somewhat complex, but generally you have the swing of it.

Since ARC does all the memory management for you, its often simpler to just use an ivar (private to the class, declared in the implementation) for your internal needs. In that case all usages just use the name.

With properties, you can as of Xcode 4.4 let Xcode synthesize the setter and getter, as well as the ivar. Auto-synthesized ivars are created with a leading "_" character.

You can define a property as readonly in the implementation, leave it as readonly, and set it in your code as '_foo = ....'. [Many on this site would consider this a bad practice, my point is you can do it.]

Xcode 4.4 has a warning titled "Implicit Synthesized Properties" with a default of NO. This creates a warning if you do not provide a @synthesize statement for each property, even though it will do the synthesis anyway.

Personally, I use ivars whenever I can, and only define properties when I need to either make something public to other classes, or I have categories declared in multiple files (in which case I put the interface declaration of the class extension in its own file along with properties defined in it.)

Upvotes: 1

FluffulousChimp
FluffulousChimp

Reputation: 9185

If you declare a property, your implementation should generally use that property even though ARC reduces some of the memory management errors.

In init some prefer to avoid using properties because doing so might trigger KVO on an object (self) that is only partially initialized.

Upvotes: 0

Ariel
Ariel

Reputation: 549

With the new Objective-C update you don't even need to synthesize the property. All you need to do is declare @property (strong, nonatomic) Var *aVar; and the compiler will automatically add the synthesizing, backing the self.aVar property with an _aVar instance variable.

Upvotes: 0

Related Questions