Monday, November 09, 2009

XCode Quick Keys or Key Short Cuts

When learning any new programming environment there will be lots of experimentation. Often being unfamiliar with the IDE becomes a frustration while trying to learn a new language like Objective C or learn new classes and libraries found in Cocoa.

Frustration with the IDE may cause you to give up. I know my impatience is the culprit!

So, here are three Quick Keys that were not obvious to me, and I was too impatient to drop down all of the menus or read the key stroke preferences to discover.

Switching between the source file and header file:
⌥⌘↑
Option Command Up Arrow (Opt Command UpArrow)

Commenting code "in" or "out":
⌘/
Command Forward Slash (Cmd /)

Move to the next argument in the auto-completion list:
^/
Control Forward Slash (Control /)



⌘ Command key
⌃ Control Key
⌥ Option Key (aka Alt Key)
⇧ Shift Key

Sunday, November 08, 2009

Objective C Method Signatures and Method Keywords

Objective C "looks" a bit different than the object oriented languages I have used previously. Object C and Pascal (Remember Lightspeed C and Pascal, single inheritance languages), C++, Java, and C#. I have not taken the time to learn Small Talk and so I do not know if Object C takes after Small talk or not.

I noticed that method signatures in Objective C have multiple "method keywords".

What is a method keyword? It is a word that proceeds the parameter type.

Here is a method from NSMutableDictionary:

-(void)setObject:(id)anObject forKey:(id)aKey

The method keywords are "setObject" and "forKey".

Because of many years developing in languages other than Objective C I find myself in the habit of decorating the method name. For example, if I have a method that inserts an object into some container and the object is stored by name then the method name would be like this:

void InsertObjectWithName (object insert, string name)

This habit shows up in my Objective C code.

@interface Foo : NSObject
{

}

-(void) insertValueWithName:(id) value : (NSString *)name;

@end

However, in Objective C you can have more than one method keyword.

@interface Foo : NSObject
{

}
-(void) insertValueWithName:(id)value :(NSString *)name;
-(void) insertValue:(id) value withName :(NSString *)name;
@end


(I intend the two methods to do exactly the same thing. Both are valid Objective C methods.)


This additional method uses two method keywords:

-(void) insertValue:(id) value withName :(NSString *)name;

At first I thought I liked either style the same. The more I use XCode the more I like the method with two method keywords because of how auto-completion reads.

Here is the single keyword style in XCode with auto-complete:



Here is the multiple keyword style in XCode with auto-complete:



For me I find that the method with two keywords reads clearer. Also I like the way the separation and highlighting of the auto-complete looks when multiple method keywords are used.

So, I am going to use multiple keywords in Objective C and avoid techniques that I use in other programming languages.

Slightly Off Topic

Syntax is just syntax and it doesn't take long to figure out the need to use brackets to invoke a method, or better said, to send a message to an object. For me it is still a bit painful to anticipate how many open brackets that I will need and I have to go back and forth in the source code inserting them as necessary.

It goes something like this:

Foo *myFoo = [Foo alloc]

Oh yeah, I better call the init...

Foo *myFoo = [Foo alloc] init]

Oh yea, I have to have another bracket at the front...

Foo *myFoo = [[Foo alloc] init]

And on and on I go back and forth inserting brackets where I need them.