views:

1901

answers:

5

You can use a standard dot notation or a method call in Objective-C to access a property of an object in Objective-C.

myObject.property = YES;

or

[myObject setProperty:YES];

Is there a difference in performance (in terms of accessing the property)? Is it just a matter of preference in terms of coding style?

A: 

As far as I've seen, there isn't a significant performance difference between the two. I'm reasonably certain that in most cases it will be 'compiled' down to the same code.

If you're not sure, try writing a test application that does each method a million times or so, all the while timing how long it takes. That's the only way to be certain (although it may vary on different architecture.)

Daniel Jennings
+3  A: 

Check out article from Cocoa is My Girlfriend. The gist of it, is that there is no performance penalty of using one over the other.

However, the notation does make it more difficult to see what is happening with your variables and what your variables are.

Misha M
+8  A: 

Dot notation for property access in Objective-C is a message send, just as bracket notation. That is, given this:

@interface Foo : NSObject
@property BOOL bar;
@end

Foo *foo = [[Foo alloc] init];
foo.bar = YES;
[foo setBar:YES];

The last two lines will compile exactly the same. The only thing that changes this is if a property has a getter and/or setter attribute specified; however, all it does is change what message gets sent, not whether a message is sent:

@interface MyView : NSView
@property(getter=isEmpty) BOOL empty;
@end

if ([someView isEmpty]) { /* ... */ }
if (someView.empty) { /* ... */ }

Both of the last two lines will compile identically.

Chris Hanson
+3  A: 

The only time you'll see a performance difference is if you do not mark a property as "nonatomic". Then @synthesize will automatically add synchronization code around the setting of your property, keeping it thread safe - but slower to set and access.

Thus mostly you probably want to define a property like:

@property (nonatomic, retain) NSString *myProp;

Personally I find the dot notation generally useful from the standpoint of you not having to think about writing correct setter methods, which is not completely trivial even for nonatomic setters because you must also remember to release the old value properly. Using template code helps but you can always make mistakes and it's generally repetitious code that clutters up classes.

A pattern to be aware of: if you define the setter yourself (instead of letting @synthesize create it) and start having other side effects of setting a value you should probably make the setter a normal method instead of calling using the property notation.

Semantically using properties appears to be direct access to the actual value to the caller and anything that varies from that should thus be done via sending a message, not accessing a property (even though they are really both sending messages).

Kendall Helmstetter Gelner
A: 

Also read this blog post on Cocoa with Love:

http://cocoawithlove.com/2008/06/speed-test-nsmanagedobject-objc-20.html

There the author compares the speed of custom accessor and dot notations for NSManagedObject, and finds no difference. However, KVC access (setValue:forKey:) appears to be about twice as slow.

charles