Mark Amery
Mark Amery

Reputation: 154545

@property vs just declaring getter and setter

Is there any difference in behaviour - either at compile time or at run time - between this code...

// MyClass.h
@interface MyClass : NSObject

@property (nonatomic) SomeType myProperty;

@end

// MyClass.m
@implementation MyClass

@end

... and this code?

// MyClass.h
@interface MyClass : NSObject

-(SomeType)myProperty;
-(void)setMyProperty:(SomeType)myProperty;

@end

// MyClass.m
@implementation MyClass {
    SomeType _myProperty;
}

-(SomeType)myProperty {
    return _myProperty;
}

-(void)setMyProperty:(SomeType)myProperty {
    _myProperty = myProperty;
}

@end

Obviously, the former version is more succinct and readable, but is there any difference in behavior? Do the synthesized getter and setter do anything more sophisticated than my straightforward implementation here? Is the declaration of a property distinguishable by introspection functions from declaration of a getter and setter? Are there any other differences I haven't thought of?

Upvotes: 2

Views: 291

Answers (3)

David Rönnqvist
David Rönnqvist

Reputation: 56625

One difference is memory management. You can configure your properties to for example copy the object being set or to use a weak variable. Your code seem to be assuming ARC is active, since you are not releasing the old object and retaining the new object.

Before ARC a typical setter would to something like

-(void)setMyProperty:(SomeType *)myProperty {
    if (myProperty == _myProperty) return;
    [_myProperty release];
    _myProperty = myProperty;
    [_myProperty retain];
}

Upvotes: 1

Tricertops
Tricertops

Reputation: 8512

Short answer: No difference. However, some property attributes (copy or atomic) may require different accessor methods.

Long answer: There is a group of introspection functions that allow you to access all @properties declared for given class or protocol:

class_getProperty
class_copyPropertyList
protocol_getProperty
protocol_copyPropertyList
property_getName
property_getAttributes

I don't think any of these functions is useful in production code, because this is basically an implementation detail of the class. Also, there may be a getter/setter exposed in the public interface and a private property hidden in class extension.

Oh, and there's one other difference: Xcode highlights properties and plain getters differently :)

Upvotes: 6

Binarian
Binarian

Reputation: 12446

When you say you use ARC, then there is only a small difference. But none that matters.

Your ivar is @protected. A @property creates an ivar which is @private.

Generally speaking: So when you subclass, it is possible for your subclass to directly access the ivar you created, but not the one the property created.

BUT since you put your ivar in the @implementation block, the ivar is never seen by the subclass.


Without ARC however and SomeType being not an Objective-C object, there is a big difference. Then your setter/getter wouldn't have retain/release messages included.

Upvotes: 0

Related Questions