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:
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:
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 *)blueColordoesn'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 ofNSColorand 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 *)blueColorcasts 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:
The
(NSColor *)aColorphrase 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:Now that won't work, unless you have previously assigned the variable
aColorto anNSColorobject, like this:Because Objective-C accepts infix nested statements, you can do this in one line, like this:
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.