Jon Sharkey
Jon Sharkey

Reputation: 73

Do Objective-C properties (self.foo) have much more overhead than using ivars (_foo)?

I prefer to write "self.foo" instead of "_foo", mostly for style reasons. In theory self.foo means a function call, while _foo doesn't. I think this ok because it's unlikely to matter much for most code.

My question is: does the compiler optimize this to the same thing anyways? Maybe it can't, due to method swizzling? Does anyone have any insight?

Upvotes: 1

Views: 92

Answers (1)

rob mayoff
rob mayoff

Reputation: 385500

No, the compiler doesn't optimize it to just an instance variable access. It cannot optimize it, because the class could have a subclass that overrides the getter method. (Note that subclasses can be created at runtime!)

However, you shouldn't worry about the message-sending overhead unless you've profiled your code and determined that it's a bottleneck.

Upvotes: 3

Related Questions