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.
Subscribe to:
Post Comments (Atom)
1 comment:
I know this is an old blog post, but with newer versions of XCode, the bracket issue becomes much less painful.
If you type:
NSObject * myObj = NSObject alloc] init]
Xcode will properly place the front braces.
Post a Comment