Life as Clay

Cocoa sorting: a brief detour

with 2 comments

One of my gripes with most of the programming books that I’ve read is that the early and ‘simple’ lessons about how to sort an array neglect multiple occurrences of the same word, capitalized differently. Cocoa Programming for Mac OS X, 3rd Edition is one of the books that ignores this point. Granted, I have no doubt the author is aware of the issue and easily could address it, but he chose not to in the text. Faced with a similar challenge in a Ruby programming book, I wrote a method that would properly handle such cases.

In the Cocoa book, Chapter 8 starts the reader on the development of the RaiseMan application. Sorting is implemented through a key path in Interface Builder, using the selector caseInsensitiveCompare:. It works great until you do the following:

Granted, the method used is caseInsensitiveCompare:. The documentation lacks a clear explanation of what this does. It sounds self-explanatory. Nevertheless, it annoys me. The first challenge at the end of the chapter is to implement a different type of sorting, using only Interface Builder. Prior to getting to that, I trolled through the documentation to find how to sort in a case sensitive manner. You might think that this would be called caseSensitiveCompare, but it’s not. I don’t know what the appropriate method to use here actually is, but I went with localizedStandardCompare: instead of caseInsensitiveCompare: and ended up with the following:

This is better, but shouldn’t the capitalized items appear before the lowercase items? That’s how it would appear in a dictionary, right? Also, what does localized mean in this context? Finally, why doesn’t the documentation include examples of how sorted/compared items will return? Including such examples would clarify all of this for me.

Therein lies my biggest problem with Cocoa so far. I understand Objective-C. I’m not an expert, by any means, but I get it and I can write foundation scripts without too much of a hassle. Cocoa isn’t clicking with me because I’m struggling to connect the dots between what I see in Interface Builder and what happens in code. For instance, caseInsensitiveCompare: and localizedStandardCompare: are methods. In code, I know how to call a method, how to pass it arguments, etc. Why are these methods called “Selectors” in Interface Builder? The sort key is personName, why isn’t the sort method caseInsensitiveCompare:? That would make more sense to me.

Chapter 8 in the Cocoa book is a deluge of unexplained Interface Builder instructions. Using NSArrayController as a controller object in the MVC framework makes sense but the steps for implementing it are difficult to grasp because what would be clear in written code becomes unclear when entered in separate text boxes in Interface Builder.

Practice makes perfect, I suppose. I’ll keep at it and I look forward to the “A Ha!” moment when it clicks and I can move forward without all of the hand holding.


Written by Clay

December 17, 2009 at 16:08

Posted in Cocoa, Code, Objective-C

Tagged with , ,

2 Responses

Subscribe to comments with RSS.

  1. I have been trying to figure out how IB and the underlying code work as well. In IB they use “selector” not “method” because in Obj-C a selector is essentially the name of the method. Check out for more on selectors. It has taken me some time poking around, and it seems that no one ever makes this clear, but if you want a custom compare method you have to put it in a category of the type that you are comparing. For instance I needed a custom compare for string objects so I made a a category for NSString and then made a method “- (NSComparisonResult)myCompare:(NSString *)aString” and then put “myCompare:” as the selector in IB. I still have a lot to learn here, but I hope this helps you understand this better.


    April 27, 2010 at 17:11

  2. Thanks, Andrew! I’ll have to revisit this challenge. I was distracted by some other work recently (grr) but hope to return to Cocoa soon.


    April 29, 2010 at 13:24

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: