Life as Clay

Cocoa Programming for Mac OS X: Chapter 8, Challenge 1

with 13 comments


Alright! Another challenge where I knew exactly what I wanted to do and spent a lot of time wrestling with the interface.

The challenge is to change the sorting on the RaiseMan column to show the names in the order of the number of characters in them. The hint is to use a key path and the “length” method of strings. Seems straightforward. In code, to find the length of the personName string, I would simply write [personName length]. Let’s see…  key values, paths, selectors, etc…..  What??

The obvious thing to do here is to return to the previous chapter and read about key paths. Except, that doesn’t help at all and may lead to confusion. (I would reference my previous post about the confusion I experience when trying to migrate coding concepts to Interface Builder.)

There’s no point in drawing this out. The proper answer is simply to make the sort key personName.length and the selector to be compare:, like this:

You have to use compare: instead of caseInsensitiveCompare: because there is no case to an integer — no lowercase and/or uppercase to compare.

I tried several other approaches, including different Selectors, and the notation discussed in Chapter 7, such as “… valueForKeyPath@”personName.length];”. I think that my general lack of knowledge of C and my inexperience with Obj-C led me to try everything but the obvious. You see, the discussion of key paths in Chapter 7 only mentioned that they could be used to find attributes of objects. Length is, of course, an attribute of a string, but the challenge mentions length not as an attribute, but as a method of Strings. I took that to mean as a method of NSString. That returned me to the initial problem – how to return the value from [personName length] to the sort key.

In the end, I don’t think that this challenge is much of a challenge because I don’t recall ever learning that I could use a method with dot notation to return an attribute as a key path. My suggestion to the author would be to specifically mention this in the “For the More Curious: Key Paths” section of chapter 7, alongside the mention of “Properties and Their Attributes,” where the author specifically derides the dot syntax and says that he won’t be using it in the book.

Still, though, I haven’t crossed the mental bridge from length being a method and length being a property and/or attribute of NSString. It must be a getter method for NSString, and hence, works with the dot syntax? The Apple developer documentation states that length is a primitive method — does this mean it can be accessed as a getter method?

While I now know that I can use this syntax for strings, I have yet to find a description of why it works or how it applies to other classes and methods. The challenge, therefore, became a guessing game and the learning lesson became frustration.

Advertisements

Written by Clay

December 17, 2009 at 17:53

Posted in Cocoa, Code, Objective-C

Tagged with , , ,

13 Responses

Subscribe to comments with RSS.

  1. Thanks for that Clay. Yeah, I’ve found a lot of this book to be merely a guessing game at times. Sometimes the author shows you what’s going on, and then fails to teach/explain what’s actually happening, assuming you’ll figure that out on your own after merely observing it. It reads well if you already know how things work, but poorly if you’re trying to learn it for the first time. Frustrating at best.

    Dave

    January 14, 2010 at 18:30

  2. One more thing, I found this book to be better at explaining Cocoa to newbies like myself: “Cocoa Programming for Mac OS X For Dummies”

    http://www.amazon.com/Cocoa-Programming-Dummies-Computer-Tech/dp/0470432896/ref=sr_1_2?ie=UTF8&s=books&qid=1263494055&sr=1-2

    Avoid the 1st edition, published in 2003 as it’s outdated. Then 2009 edition is more modern.

    Caveats include there are a LOT of errors in the code examples, usually a couple per chapter. But I found them rather easy to figure out and finished the book in about 2 weeks with all examples complete. It’s definitely a beginners book, where-as Aaron Hillegass’s is more intermediate. You’re stepped through things carefully and most stuff is explained rather well.

    I loved Dave Mark’s “Learn C on the Mac” book. He helped write “Learn Cocoa on the Mac” which should be coming out soon (Jan 2010 is the current release date). Another Cocoa book to check out.

    Dave

    January 14, 2010 at 18:40

  3. Thanks, Dave! I’ll look into those. Cheers!

    Clay

    January 14, 2010 at 19:34

  4. Thanks for the help. However, when I try your method, it says in the log:
    2010-01-19 17:41:09.599 RaiseMan[754:a0f] -[_NSControllerObjectProxy copyWithZone:]: unrecognized selector sent to instance 0x21f800
    2010-01-19 17:41:09.604 RaiseMan[754:a0f] -[_NSControllerObjectProxy copyWithZone:]: unrecognized selector sent to instance 0x21f800
    2010-01-19 17:41:09.612 RaiseMan[754:a0f] -[_NSControllerObjectProxy copyWithZone:]: unrecognized selector sent to instance 0x143bc0
    2010-01-19 17:41:09.612 RaiseMan[754:a0f] -[_NSControllerObjectProxy copyWithZone:]: unrecognized selector sent to instance 0x143bc0
    2010-01-19 17:42:58.965 RaiseMan[754:a0f] -[_NSControllerObjectProxy copyWithZone:]: unrecognized selector sent to instance 0x22a070
    2010-01-19 17:42:58.965 RaiseMan[754:a0f] -[_NSControllerObjectProxy copyWithZone:]: unrecognized selector sent to instance 0x22a070

    I mostly had to guess what to do here, too. I couldn’t figure it out and searched the internet for some help. Hoping if I saw the code, I could understand. I think I’ll look into the dummy-book.

    Timm

    January 19, 2010 at 16:47

    • make sure you are using –

      compare:

      (with the colon)

      David Wang

      February 3, 2010 at 16:23

  5. Hmm, I’d have to take a look at your project to see the differences. Sorry it’s not working for you!

    Clay

    January 19, 2010 at 17:05

  6. I also think this looks rather promising:
    Beginning Mac Programming
    http://oreilly.com/catalog/9781934356517/#related-content
    It’s slated for a February 2010 release.

    Timm

    January 19, 2010 at 17:10

    • I picked up this book… it’s really for beginners. Oddly, though, it discusses some esoteric concepts prior to really diving into simple code examples. Pointers, for instance, aren’t exactly an intuitive topic, even to people who program. They are discussed early in the book. I understand why the author went that direction, but nevertheless, I found it out of order. The book doesn’t really hit its stride until about 1/2 way through.

      That said, it’s really not a good next step for people who have worked through the Hillegass book because it’s so basic. I’m glad that I picked it up but probably wouldn’t buy it again, knowing what I know now.

      Clay

      April 16, 2010 at 00:55

      • I picked it up too and boy have I learned a lot! I really appreciated the completely down to earth explanations.
        The first Cocoa book I bought was Hillegass’ and prior to that, I hadn’t done any programming (it still lies on the shelf, I’m halfway through).

        So knowing what I now know, I’ll pick Hillegass’ book up again. Or finish “Learn Cocoa on the Mac,” which I’m halfway through. Or buy a book on Cocoa Touch/UIKit.

        Timm

        April 16, 2010 at 20:04

  7. That’s all right. I just appreciate the helpful information.

    Timm

    January 19, 2010 at 17:21

  8. No problem. I would like to upload the xcode projects, but wordpress.com won’t let me, unfortunately. I’ll see if I can drop it on Dropbox or something.

    Clay

    January 19, 2010 at 17:25

  9. I went down the same route, using personName.length. But then I figured that the author must’ve mention key paths for a reason, and as he said he wasn’t going to rely on the dot method, I knew there was another way to do it.

    I ended up doing:

    [personName valueForKeyPath:@”length”]

    Great post, Clay :-). Nice to see someone else who’s following the same thought process as I am.

    Chris Neal

    April 27, 2010 at 09:45

  10. I just completed this challenge as well and found the hint as given to be a bit vague. Since the author is pretty dismissive of dot notations earlier it might be good to give a hint about using something that he had earlier declared as, “…a rather silly addition to the language…”.

    Then again it’s supposed to be a _hint_ I suppose and not hand holding.

    macshome

    September 1, 2010 at 23:26


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: