Re: Newbie Question on "self"
Re: Newbie Question on "self"
- Subject: Re: Newbie Question on "self"
- From: Matt Neuburg <email@hidden>
- Date: Mon, 11 Aug 2008 11:05:55 -0700
- Thread-topic: Newbie Question on "self"
On Mon, 11 Aug 2008 17:47:59 +0000, Matt Keyes <email@hidden>
said:
>Hello again,
>
>In C/C++ and the .NET languages I am used to, I have generally tried to prefix
any member variables inside class methods with "this"
>
>i.e. this.m_sMyString = "this is my string";
>
>In Objective-C, this doesn't seem as clear to me (or at least it doesn't
compile). For example:
>
>- (id)initWithString:(NSString *) string {
> self.myString = [[NSString alloc] initWithString:string];
>}
>
>Produces an error:
>
>error: request for member 'myString' in something not a structure or union
If myString is an ivar you can access it by saying self->myString.
Objective-C is C! (And it is not C++.)
<http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articl
es/chapter_3_section_4.html#//apple_ref/doc/uid/TP30001163-CH12-BAJHEIFE>
There is a religious war over whether you *should* do this, though.
Personally I like it and I do it a lot, because it reminds me that this
thing (myString) is an ivar. Furthermore, this religious war is also bound
up with a religious war over accessors; some will say that you should always
implement an accessor and say
[self setMyString: whatever]
instead. Personally, I know when I need to pass thru the accessor for
memory-management or KVC and when I don't, so there are plenty of times when
I still say self->myString. m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
One of the 2007 MacTech Top 25: <http://tinyurl.com/2rh4pf>
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden