Because there is no instance variable named textField, but there is one named 
_textField.

The property declaration makes available calls [self textField] and [self 
setTextField:x] which are the same as self.textField in get (x = 
self.textField) and set (self.textField = x) operations, respectively, and are 
method calls. The synthesize makes available self->_textField which can be 
shortened to _textField within the class's instance methods; this is the actual 
storage, not a method. If you left off the explicit name within the synthesize 
call, or even the prepended underscore, you could make all these look the same 
although they would still behave as described.
--
Gary L. Wade (Sent from my iPad)
http://www.garywade.com/

On Dec 29, 2011, at 6:07 PM, Peter Teeson <p.tee...@bell.net> wrote:

> 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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to