About
This is the M Cubed Software weblog. To find out more about us head to our about page.
Search
Feed
Archives
- September 2010
- June 2010
- April 2010
- March 2010
- February 2010
- January 2010
- November 2009
- August 2009
- July 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
The Named Argument
Posted on 31/05/2009 at 03:29 PM in
Thanks to the iPhone and the growth of the Mac there are a lot of people now interested in Objective-C and Cocoa. Many of these developers come from other backgrounds such as Python, Ruby, C#, C++, Java etc, but Objective-C is quite a different beast to those languages.
Of course there are quite a few similarities. It shares the C heritage of C++, it is highly dynamic like Python and Ruby. However, the biggest difference between Objective-C and most other languages is how method names and arguments interact.
At first glance a lot of developers coming from other languages think that they are simply named arguments which is wrong, VERY wrong. This is one of my pet peeves about new Objective-C developers, and every time I see someone talking about named arguments in Objective-C I always feel a need to correct them. But if they aren't named arguments then what are they?
Interspersed Arguments
The arguments aren't named, but interspersed with the method name. For example, a method in another language may be obj.foo(namedArg1=bar, namedArg2=possum), so its method name is foo. In Objective-C a similar method may be [obj foo:bar with:possum]. In this case the method name is foo:with:. So the arguments aren't named, they are mixed in with the method name.
There are some key differences between named arguments and interspersed arguments:
1) Named arguments' names are not part of the method name
2) Named arguments can be re-arranged
3) Named arguments are often optional
None of these hold true for interspersed arguments. This is why I've seen quite a few people complain about Objective-C's "named arguments" not being very flexible. When you realise they aren't named then things get much easier to understand. As with everything in Cocoa and Objective-C, if it is different to what you are used to, then there is likely a very good reason for it being that way.
Examples
So it would make sense to show some examples comparing named and interspersed arguments. So lets take a common Objective-C method:
[@"A string" compare:@"B string" options:NSCaseInsensitiveSearch range:someRange];
And now lets convert it to a language like Python which has named argument, treating the arguments as named arguments:
"A string".compare("B string", options=NSCaseInsensitiveSearch, range=someRange);
Not a big difference at first, but lets try some variations:
"A string".compare("B string", range=someRange, options=NSCaseInsensitiveSearch);
"A string".compare("B string", options=NSCaseInsensitiveSearch);
In Python those are both the same method as the first one. However, in Objective-C, re-arranging the arguments or removing them would result in an entirely different method being called. While in the Python examples the method is called compare() in the Objective-C example it is called -compare:options:range: which is a big difference. If we correctly treat the arguments as interspersed then the Python example becomes:
"A string".compare_options_range("B string", NSCaseInsensitiveSearch, someRange);
Of course Python doesn't support interspersed arguments so they are all attached at the end, but basic rules are the same. Hopefully this has helped quite a few Objective-C newbies to understand one of the many differences between it and other languages they are used to, but if you have any questions then leave them in the comments.
(5) Comments
Going Full Time
Posted on 28/05/2009 at 11:42 PM in
So I've done it. I said in November that I wanted to be able to go full time with M Cubed in about 7 months. 7 months later I'm going full time. It's been a long time coming but I have finally finished my full time education and can now concentrate fully on M Cubed.
M Cubed has been a part time venture ever since it was founded back in 2005. It always came second to exams and assignments and homework. Because of this releases have been far and few between and often come in spurts when I've had a lot of free time. In the past 3 and a half years I have released just 10 major updates to apps. I'm wanting to seriously improve on that by getting major updates to all my current apps out by the end of the year, and possibly a new app or two.
With all the extra time I will have now, you can expect to see a lot more open source efforts from M Cubed as well as more blog posts discussing various technical things. It will be nice to be able to dedicate time to give back to the community that has helped me get to this point.
And finally, there is some incredibly huge news still to come, all will be revealed on the 1st of July, so watch this space.