On Mon, Apr 21, 2008 at 1:27 PM, Scott Andrew <email@hidden> wrote:
> I have a question.
>
> I am getting ready to teach a bit on Objective-C 2.0 to colleagues and I
> have a bit of a style question.
>
> If i have the following defined:
>
> @interface foo : NSObject
> {
> @private
> NSString* myString;
> }
>
>
> @property(copy) myString;
>
> -(void)displayString;
>
>
> @end
>
> Which way is better to use myString in the displayString function? Is it
> better to do self.myString or use myString directly.
Personally, I always use the accessors unless I have a reason not to.
In fact, when building for the 64-bit runtime (or any other platform
that uses the non-fragile-ivar runtime), I try to avoid declaring the
instance variables altogether, that way, I know that *all* access
happens through the accessors.
The two exceptions to this strategy:
1) In performance-critical code, where you can't afford the overhead
of a method-call for each access of the property, you'll have to
access the ivar directly
2) When the accessor has to do something more than just get or set the
variable, you will need access to the ivar within the accessors that
you write. In which case, you obviously need to declare them.
In the end, it comes down to a matter of personal preference.
> Comming from a C++ part of me says to always use the acessors if defined
> (unless private and not exposed), also using the accessors allows us to make
> sure our bindings are updated if its a bound value, but i see a mix of code
> and it may depend on if the iVar is to be exposed or not.. The company i
> work for is now getting into iPhone and 10.5 development and i want to
> stress the proper design choices.
--
Clark S. Cox III
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/objc-language/email@hidden
This email sent to email@hidden