An introduction to Cocoa and Cocoa Touch
If you know someone who’s recently taken up programming the Mac or the iPhone (or both), please give them one (or both) of these links:
As a frequent answerer of questions on Stack Overflow, I see certain patterns—not just frequently-asked questions, but also frequently-unasked questions: Things that are biting the questioner that they don’t know about.
Sometimes they’re thinking about things the wrong way (e.g., using property list objects where they should have a model). Sometimes there is a route to a solution (“my app is slow” “run Instruments”), but they don’t know about that route. Often, they’ll use wrong terms that are right on another platform (like “member variable”), like a speaker using their native language’s pronunciation rules when speaking a foreign one.
To help people over these speed bumps, I’ve composed an introduction for new Cocoa and Cocoa Touch programmers. (Both of the above links redirect to it.)
If any part of it doesn’t help them (i.e., is unclear or wrong), I’d appreciate it if you’d let me know, either here or by email.
February 3rd, 2010 at 12:28:58
Great collection of bullet points that every Cocoa developer should internalize. A handful of others come to mind (of course it’s up to you where to draw the line — otherwise the list would never end).
Regarding memory management:
* “Ownership” has a specific meaning here that is not quite like the non-programming meaning of the word. An object can have multiple owners, and be owned multiple times by the same owner.
* You should be aware of the possibility of retain cycles.
Regarding Objective-C (though it seems you’re addressing framework issues more than language issues):
* Contrary to common misconception, methods do not have “named” parameters. If they did you would not be able to have a method called performSelector:withObject:withObject:. Which parameter would be the one named “withObject”?
* It’s perfectly okay to send messages to nil, and to simplify code accordingly. This is something that people new to Objective-C seem to have trouble getting their heads around.
February 3rd, 2010 at 12:54:22
That’s true in real life as well. Things, both physical and imaginary, can be co-owned by multiple owners.
(IANAL.)
True, but unusual. Even so, important to conceptual understanding. I’ll think about how to include this.
Good suggestion.
Both of them.
I get what you’re saying, though:
performSelector:withObject:withObject:
is the complete name (selector) of the method, not simply the name followed by two arguments. This is worth including.Also, a variable will not magically become
nil
if the object whose pointer is in it dies. As such, you can’t test the variable whether the variable containsnil
to test whether the object is still alive. (This is most likely from people conflating the variable with the object.)Good suggestions all. Thank you.