Stas Jaro
Stas Jaro

Reputation: 4885

Manually implementing @property

When manually implementing @property instead of using @synthesize, do you have to include ARC code?

Would it be ok to implement it like this:

@synthesize var1;

- (void)setvar1:(NSObject *)newVar1
{
    var1 = newVar1;
}

or do you have to include retain, release etc?

Upvotes: 3

Views: 177

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

What you call "ARC code" (retain, release, etc.) is actually manual reference counting, not automatic.

If you are compiling with no ARC, you need to retain or copy the object as required. If you are under ARC, the compiler will take care of it for you. Specifically, the compiler will retain newVar1 if var1 is declared __strong.

Upvotes: 1

Greg Hewgill
Greg Hewgill

Reputation: 992747

When using ARC, you cannot write code that manually uses retain, release, and so on. Therefore, if you choose to implement your property getters and setters manually, and you have ARC enabled, you don't have to include that extra memory management code.

Upvotes: 1

Tim
Tim

Reputation: 60110

Under ARC, you don't have to (and in fact cannot) manually retain or release variables. Your implementation, apart from needing a capital V in setVar1:, is perfectly acceptable under ARC.

Upvotes: 2

Related Questions