A Roadmap Through Cocoa

There's a comment on the post for the first episode of Late Night Cocoa that caught my attention. The comment is basically a request for a roadmap through Mac OS X programming:

One thing that did resonate with me was the comment on how many frameworks you had to get your head around and how long it took to get your first application up and running.

Maybe a roadmap [...] could be discussed here so experienced Cocoa programmers can help steer novices through this "forest" (thats what is seemed to me when I first started).


Do a lot of people feel this way? The thing that makes it hard to answer something like this is that there are a lot of frameworks, meaning there are a lot of possible roadmaps. If you're interested in making a game, you're going to have a different roadmap than someone making a mail client.

In other words, you don't need to learn all of the frameworks before you starting working on an application. In fact, choosing an application to work on might be a better guide than some sort of formal plan.

There are basics, of course. You need to understand Objective-C, and the basic ideas behind Foundation and AppKit classes. Key-Value Coding is the basis for a number of things, so that's important too.

If you want to manipulate data, Core Data can be an invaluable tool. After that, though, the field is wide open. Maybe your application benefits from WebKit, maybe it needs AddressBook. It just depends on what you're looking to do.
Design Element
A Roadmap Through Cocoa
Posted Jan 20, 2007 — 34 comments below




 

Ben — Jan 20, 07 3358

Yeah, it's a tough question because everyone's roadmap will be a little different, based on not only what kind of software you're planning to write, but also where you're coming from. (e.g. if you're a new developer, you'll need to spend a bunch of time understanding both C/ObjC and OO concepts before you can do anything useful. And if you're coming from another platform, you'll have to learn slightly different ways of doing old tricks.)

FWIW, the intro roadmap that worked great for me was going through the Hillegass book, chapter by chapter and challenge by challenge, which really gets you through all the fundamentals, and then studying the example Sketch app, which is a good primer on application architecture. (There still isn't anything in print for learning CoreData or CoreImage.)

Kyle — Jan 20, 07 3359

You know the hardest part for me when beginning? Learning how to break my application up into different source files.. do i put all my controller stuff in one source, do i put my delegates into another?

What if I need to access different classes from other classes? What's the best design for that kind of thing. Cocoa is a bit different and under normal conditions I'd understand this, but Cocoa confuses me in this respect.

Scott Stevenson — Jan 20, 07 3360 Scotty the Leopard

There still isn't anything in print for learning CoreData or CoreImage

I believe Step in Xcode covers Core Data.

Scott Stevenson — Jan 20, 07 3361 Scotty the Leopard

@Kyle: What if I need to access different classes from other classes? What's the best design for that kind of thing. Cocoa is a bit different and under normal conditions I'd understand this, but Cocoa confuses me in this respect
There's no one rule for things like this. It really depends on the what your goal is. I don't even think it's something that's specific to Cocoa. Figuring out the design of things seems to come from experience more than anything.

That said, I get the question about "accessing different classes from other classes" quite a bit, and it usually ends up that the question itself is beside the point. Usually, there's a simpler way to what you want. Again, it just depends on the particular case. Feel free to post a simple example here if you want.

Richard Albury — Jan 20, 07 3362

I'll second the Hillegass book, followed by the Garfinkel/Mahoney book (a bit dated, unfortunately, followed by cocoadevcentral and looking through all the Apple sample code. Coming from the dark side, it is a bit hard to find your way around at first, but it clicks after a while.

I believe Step in Xcode covers Core Data.[/]

s/in/into/

Peter Hosey — Jan 20, 07 3363

Generally, when you have two objects that don't know about each other (neither one owns, is a delegate of, or is otherwise related to the other), the question of "how do I get the other object to do something/get information from or to the other object" is the wrong question. To get it to do something, use NSNotification. For passing information around, you usually have a third object set up KVO. (You are writing accessors, right?)

Rob — Jan 20, 07 3364

A visual road map would be very nice.

Images of the typical interface implemented by the API or a flow chart for data or processes.

Stepping through from simple to complex, would be very helpful.

All the structs for simple to complex, grouped by simularity, with links to the Apple help page would be nice.

Notice how everyone is asking about their specific problems and can't express what they want specifically, if we had a road map then we could speak the same language and ask in terms of the APIs.

Chris L — Jan 20, 07 3365

I was an experienced Windows & Unix programmer. It was tricky to learn the idioms of Objective-C, especially KVC and the memory management / reference counting rules. No harder than COM, but different... easier to use once you get used to it. Also it was very confusing to learn how to hook up objects to UI with Interface Builder, that was pretty confusing compared to Windows Forms which is more automatic. The Cocoa frameworks are not too hard after you learn those basics.

Todd — Jan 21, 07 3366

I also run into the problems Kyle mentioned. I often get the "can't get there from here" feeling, whether it's getting access to an object or getting data back to a different part of the app. Any recommended reading, to help get the big picture view of Cocoa?

A recent example: I have a client app, which sends commands to a server based on user actions. To get all keyboard input, I subclassed NSView and used keyDown to get the keyboard input. But, then I don't see how to get the keypresses back to my object that has the network connection, to send the command.

Peter Hosey — Jan 21, 07 3367

Todd: Like I said above, that sort of thing is what NSNotification is for.

Scott Stevenson — Jan 21, 07 3369 Scotty the Leopard

But, then I don't see how to get the keypresses back to my object that has the network connection, to send the command

You could either do what Peter says about NSNotification, or you could create a "_delegate" instance variable, so that your view does something like this in keyDown:
delegate = [self delegate]; if ([delegate respondsToSelector:@selector(viewDidReceiveRequest:)]) { [[self delegate] viewDidReceiveRequest:self]; }

And the delegate implements the method:
- (void)viewDidReceiveRequest:(id)theView { // do something with the network here }

The reference to delegate should not be retained. This is called a "weak reference."

Jonas Greitemann — Jan 21, 07 3371

It seems we've a Q&A hour today, so why do you prefix instance variables with an underscore ("_delegate") and nevertheless use "delegate" inside the method, Scott? This kind of naming conventions are very confusing to me.

With me coming from Java, I know this "can't get there from here"-feeling, as Todd calls it, very well. Cocoa's notifications and the strict separation of classes into Model, View and Controller are a huge advantage, so I don't have this problem any more.

Todd — Jan 21, 07 3373

Oops, I think a reload submitted my comment again. Sorry.

Thanks for the responses. I've used nsnotification in another project, so I'm familiar with that. It just seemed like it was overkill for what I thought would be a simple thing.

I haven't created my own delegate methods before.. So, I think I'll give that a try & read up on it some more.

Thanks!

Scott Stevenson — Jan 21, 07 3375 Scotty the Leopard

so why do you prefix instance variables with an underscore ("_delegate") and nevertheless use "delegate" inside the method, Scott? This kind of naming conventions are very confusing to me

A leading underscore usually means "private." All instance variables are implicitly private in Objective-C, so many experienced user simply prefix all of them with an underscore. Part of the reason is to avoid confusion with local variables. For example, take a look at this:

@interface DefinesTest : NSObject { id delegate; } @end @implementation DefinesTest - (void) talkToDelegate { id delegate = nil; } @end

This is, at best, simply confusing. Is the delegate declared in talkToDelegate a local variable or does it refer to the instance variable? If you add an underscore to the instance variable, you can easily tell the two apart.

Peter Hosey — Jan 21, 07 3376

Scott: GCC will give you a warning if you try that (local variable declaration shadows instance variable declaration).

My solution is to not give my local variables the same name as my instance variables. (In -initWithFoo: and -setFoo: methods, I name the argument variable newFoo.)

Scott Stevenson — Jan 21, 07 3377 Scotty the Leopard

Scott: GCC will give you a warning if you try that (local variable declaration shadows instance variable declaration)
Well it certainly used to. I just tried it on a stock Cocoa project template on Xcode 2.4 and it doesn't say a thing about it.

Brian Gilstrap — Jan 22, 07 3379

I have to agree with Chris L's comments: "It was tricky to learn the idioms of Objective-C, especially KVC... Also it was very confusing to learn how to hook up objects to UI with Interface Builder..."

I have many (20+) years of serious development experience, including writing complex GUIs in Java. Before Java, I was doing C++ for five years of serious distributed systems development. I've been doing OO programming for over twenty years.

I want to use Interface Builder and key-value binding, since there is clearly a lot of power to avoid writing glue code. But I've found nothing that really pulls the concepts together in a good tutorial. Right now my biggest issue is knowing what can be connected to what in IB. Is there some crucial documentation that I've not found that describes the foundation classes in relation to key-value binding/coding?

Rob — Jan 22, 07 3381

Has anyone ever built a Quick Reference Guide for the API pallet in Interface Builder ?

That might be a good place to start.

Nick Rundquist — Jan 22, 07 3385

The thing that makes it hard to answer something like this is that there are a lot of frameworks, meaning there are a lot of possible roadmaps.

There are a lot of ways to get from Austin to Houston but you only need one map to see them all. I think what people are looking for is something less like the "roadmaps" we're used to in the tech industry, which tend to be one dimensional, and more like an actual two dimensional roadmap where you can see what your choices are. You say that there are a lot of frameworks but it's not even easy to find out what they are. The best list I could find is this. I don't know what others are looking for, but I've always wanted to see something along the lines of what Sun produces for Java. Indeed in general I have to say that I think Sun does a much better job structuring its Java documentation than Apple does its Cocoa documentation and it's this lack of structure that leads to a feeling of being lost in the forest.

Scott Stevenson — Jan 22, 07 3386 Scotty the Leopard

The best list I could find is this.
You wish that's all there was. :) I kid. The catch for Apple is that developers are constantly asking for more API to work with, so they have more to document.

In my opinion, the goal shouldn't be to get a comprehensive undestanding all of your options (at least not at first). To me, this is like somebody walking up and asking what all the options are for careers. I wouldn't even know where to start. I would need to have some idea as to what your interests are before I could make suggestions. Once you can give some sort of idea about what you want to do, it becomes much easier to make suggestions about how Cocoa and the related frameworks can help you do it.

I've always wanted to see something along the lines of what Sun produces for Java.
Do you have some examples? I'm open-minded to considering Sun has some good ideas here, but I'm not sure Apple and Sun really have the same sort of needs to address.

Nick Rundquist — Jan 23, 07 3387

You wish that's all there was. :)

Looking in /System/Library/Frameworks/ I see your point. Now I want a roadmap just so I don't take a wrong turn and end up in the DiskArbitration framework.

To me, this is like somebody walking up and asking what all the options are for careers. I wouldn't even know where to start.

Well, there is a giant book of careers put out by some branch of the US government, or at least there was when I was in high school. And I did peruse it out of curiosity. It's certainly not out the realm of possibility that I'm unique in my fondness for poking around in well structured data.

I'm not sure Apple and Sun really have the same sort of needs to address.

True enough. Since I wrote my initial post I've realized that what I liked about the Java documentation so much was that, being generated by JavaDoc, it shared its structure with the classes it was documenting. Lacking the notion of packages this doesn't translate over to Cocoa so well.

That said I still feel that beginners could benefit from some facile treatment of a broad range of subjects. Not necessarily explaining anything in depth, just pointing to the most appropriate documentation on the subject. How best to go about that, however, escapes me right now.

Peter Hosey — Jan 23, 07 3390

I just tried it on a stock Cocoa project template on Xcode 2.4 and it doesn't say a thing about it. —Scott

Not so. The trick is that it gives the warning on the use of the shadowing local variable, not the declaration of it.

- initWithFoo:(NSObject *)foo { //No warning here if((self = [super init])) { [foo bar]; //Warning here } return self; }

Rob — Jan 23, 07 3394

I have a request, hopefully I am close enough to the topic here.

I hope you understand my langauge I am trying to be specific and in langauge geeks would understand.

Would it be possible to do a tutorial on how to implement Mole Code in Cocoa, I don't want it for hacking, it would probably need to be more capable for that, just for testing.

But that example could probably be used for internal app scripting and messaging so could have general use.

I would make a generous donation, $40?, for that too since it applies to my work.

mmalc — Jan 23, 07 3395

It would be interesting to know in what ways Apple's Getting Started with Cocoa does not provide a roadmap.

mmalc

Scott Lewis — Jan 31, 07 3468

This is probably somewhat off-the-wall compared with the general texture of these comments on learning Cocoa, but what I found most useful in learning/continuing to learn Cocoa was exploring Smalltalk more thoroughly.
It could be argued that much of what Obj-C/Cocoa/Xcode is doing is creating a better Smalltalk. I don't mean just the language, but the entire programming environment.
Dealing with the way Smalltalk handles the user interface clears up many of the mysteries of how Interface Builder works, as IB seems (to me) to be designed to overcome some Smalltalk limitations. Similarly, Smalltalk makes KVC seem very reasonable, as the alternative is a kind of "ritualistic" coding.
Also, exploring Smalltalk can help make your code much more fully object-oriented. For example, classic Smalltalk does not have anything like a "case" statement. Why? Because in classic O-O coding, you should always use polymorphism instead. Each object should know what to do when sent a standard message.
I would note, though, that I come from a background of studying English Lit, rather than Math or Science, so this could be a very idiosyncratic approach!

Rob — Jan 31, 07 3469

Is there an equivalent to the following in Xcode:

try
{ do something that can fail }
on error
{ do error alert or do nothing }

Scott Stevenson — Jan 31, 07 3470 Scotty the Leopard

@Rob: Do you mean Objective-C? Check out the page on exceptions.

Rob — Feb 01, 07 3476

C and C++ also seem to work well in Xcode ...

... THANKS for the pointer to the @try in O-c that should work well.

And thanks for the way cool handle !!!

Rob — Feb 01, 07 3477

Here is a definite road map question.

What is the easiest way to get work Xcoding Cocoa, low Cocoa requirements, 20 years test engineer experience ?

Rob — Feb 01, 07 3478

How do I do this ???

"To turn on support for these features, use the -fobjc-exceptions switch of the GNU Compiler Collection (GCC) version 3.3 and later."

Scott Stevenson — Feb 01, 07 3479 Scotty the Leopard

@Rob What is the easiest way to get work Xcoding Cocoa, low Cocoa requirements, 20 years test engineer experience
The Learn Cocoa tutorial might be a good start, along with with the C Tutorial. If you want a book, Cocoa Programming for Mac OS X by Aaron Hillegass is very good.

How do I do this? "To turn on support for these features, use the -fobjc-exceptions switch"
You just need to click the checkbox for "Enable Objective-C Exceptions" in build settings. The build settings panel is described here.

Rob — Feb 02, 07 3481

Thanks, I found it in the Project/File info, "Styles" -- I am in 10.3.
Although, while it now compiles it is still crashing ! I guess it took a while implement fully. I wish they would ship the flash laptops and Leopard so I could get up to date.

Actually, my other question was do you know of any places that are hiring O-c programmers, people that have a lot of Mac experience as say a tester but are just getting into O-c.

Thanks

Scott Stevenson — Feb 02, 07 3482 Scotty the Leopard

@Rob: do you know of any places that are hiring O-c programmers, people that have a lot of Mac experience as say a tester but are just getting into O-c
I don't know of any personally. You should keep an eye on the CocoaDev jobs board.

Rob — Feb 09, 07 3560

Hey, I just ran into this by accident its an image editor written with Cocoa and the source code is available, it might be cool to review the code on your site. I haven't looked at it yet, just found it ...

http://seashore.sourceforge.net/download.php




 

Comments Temporarily Disabled

I had to temporarily disable comments due to spam. I'll re-enable them soon.





Copyright © Scott Stevenson 2004-2015