Re: Interference of a variable name with Interface Builder
Re: Interference of a variable name with Interface Builder
- Subject: Re: Interference of a variable name with Interface Builder
- From: Kyle Sluder <email@hidden>
- Date: Sat, 12 Jun 2010 14:22:13 -0700
On Sat, Jun 12, 2010 at 2:11 PM, Jochen Moeller <email@hidden> wrote:
> Hello List,
>
> Normally variable names don't interfere with method names. So I can write:
Strictly speaking, Variable names can never interfere with method
names. The compiler determines the type it needs from context, and
then resolves the symbol. There is no instance in which either a
method or a variable name can be used.
>
> CGFloat alphaValue = [ myCustomView alphaValue ];
>
> Now, in an app the -drawRect: method of my custom view was not called although it should. I found out that the culprit was an outlet named "alphaValue" which is also an NSView method. Here is the tracked down example.
>
> Xcode 3.2.2, IB 3.2.2, GCC 4.2, Architecture: 10.6 | Debug | x86_64
> A new Cocoa App with the following NSView subclass:
>
> // MyView.h
> #import <Cocoa/Cocoa.h>
> @interface MyView : NSView {
> IBOutlet NSTextField *alphaValue;
> }
> @end
>
> // MyView.m
> #import "MyView.h"
> @implementation MyView
> - (id)initWithFrame:(NSRect)frame {
> NSLog(@"%@", NSStringFromSelector(_cmd));
> self = [super initWithFrame:frame];
> if (self) {
> // Initialization code here.
> }
> return self;
> }
>
> - (void)drawRect:(NSRect)rect {
> NSLog(@"%@", NSStringFromSelector(_cmd));
> [[ NSColor whiteColor ] set ];
> NSRectFill(rect);
> }
> @end
>
> In IB drop a custom view and assign it to MyView.
> Then drop a label into the window without connecting it.
> In Xcode Build & Run -> -drawRect: method is called and a white rectangle is shown.
>
> Now connect in IB the label with the outlet of MyView and Run again.
> -> -drawRect: is not invoked, no rectangle is drawn, and no error or warning appeares.
>
> Funny, or ?
The nib-loading machinery will call KVC-compliant accessors when
unfreezing a nib. Since NSView is KVC-compliant for the key
"alphaValue", the nib loading machinery will call -setAlphaValue: with
your text field as an argument.
So this bug is your responsibility; you have overloaded a term with an
existing meaning. The solution is to rename your outlet.
--Kyle Sluder
_______________________________________________
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