Re: Code error in Your First Mac App tutorial..
Re: Code error in Your First Mac App tutorial..
- Subject: Re: Code error in Your First Mac App tutorial..
- From: Peter Teeson <email@hidden>
- Date: Thu, 29 Dec 2011 21:07:22 -0500
Thanks for your reply Ken. But I still don't understand. See bottom post.
On 2011-12-29, at 5:23 PM, Ken Thomases wrote:
> On Dec 29, 2011, at 3:38 PM, Peter Teeson wrote:
>> I'm familiarizing myself with Xcode 4.2.1 under Lion 10.7.2 so am doing the "Your First Mac App" tutorial.
>> This code snippet from the tutorial is incorrect and causes an error in compilation.
>> if (sender == textField) { senderName = @"textField"; }…….
>> The error is "Use of undeclared identifier 'textField'; did you mean _textField?"
>> The correct code is:
>> if (sender == self.textField){senderName = @"textField"; }
>> In fact if you refer to any of the synthesized iVars such as 'window' or 'slider'
>> or indeed any that you care to make up for yourself (eg aardvark) the same issue occurs.
>> I don't know if this is a scope, language or compiler issue.
>> Why would one need to use self.textField?
> This is just an issue with the tutorial. Those properties are @synthesized with an explicit instance variable name, which is different from the property name due to the underscore prefix:
> @synthesize textField = _textField;
> You don't necessarily have to refer to "self.textField". You could have changed that line to be:
> if (sender == _textField){senderName = @"textField"; }
> The problem with the original is that it referred to an identifier "textField" which didn't correspond to any declared variable. The instance variable is called "_textField". And property names are not accessible just as naked identifiers; they need to be accessed using Dot Syntax (or, of course, using the getter or setter names or Key-Value Coding).
> It was good of you to give that feedback to the document maintainers.
> Cheers,
> Ken
The declaration in the Interface .h file, generated by making the Outlet connection. is:
@property (weak) IBOutlet NSTextField *textField;
To me that @property statement gives the explicit name textField.
The generated @synthesize statement in the Implementation file is the one that assigns _textField isn't it?
I understand that I could have used _textField. In fact in my research I tried it and it worked.
Similarly for the other iVars window/_window and mute/_mute.
Doing the digging is what led to me conclude there is some general rule which I do not yet know.
I understand there is a special meaning for vars beginning with under bar but I still have to refresh my memory on
Obj-C 2.0.
So I repeat my question; can someone please explain why I had to use self.textfield?
Thanks…
Peter
_______________________________________________
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