Re: TextField Background Color

Hi,

Each language and API is different, and each one takes time and energy to learn. It is natural to find what you are familiar with to be preferable to that which is new, especially while in the initial phases of learning. Learning can be frustrating. Persistence pays off, though - hang in there.

As for your problem, I can see the reason for your difficulty: (NSColor *)blueColor doesn't do what you think it does. You actually mean to say [NSColor blueColor]. Let's look at the difference between these two statements.

[NSColor blueColor] calls a class method of NSColor and returns a default blue color object. Remember, all messages need to be put inside square brackets. Write the receiver name, then the message to be sent, which must be a method implemented by the receiver.

On the other hand, (NSColor *)blueColor casts the variable blueColor as an NSColor object, but does not return anything. In this context, this doesn't really make any sense. It does nothing to change the actual value stored in the variable, which will be nil unless the variable has already been assigned.

I can guess why you are getting this wrong, though. When you see this method declaration in the documentation, it is written like this:

- (void)setBackgroundColor:(NSColor *)aColor

The (NSColor *)aColor phrase simply means that the value passed after the : must be an NSColor object. To call this method in your code, you do so like this:

[textField setBackgroundColor:aColor];

Now that won't work, unless you have previously assigned the variable aColor to an NSColor object, like this:

NSColor * aColor = [NSColor blueColor];

Because Objective-C accepts infix nested statements, you can do this in one line, like this:

[textField1 setBackgroundColor:[NSColor blueColor]];

That saves you one line of code and a variable assignment, but it is more difficult to understand.

Submitted by alex on Tue, 12/08/2009 - 05:12.

Reply

The content of this field is kept private and will not be shown publicly.
Enter the code shown in the image:

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
5 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Donate!





If you like what you find here and wish to support further development of this site, please donate via PayPal. No account required.

Syndicate

Syndicate content

User login

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
1 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
Enter the code shown in the image: