Best way to learn to master the AppKit framework - objective-c

I've currently been learning Objective-C and Cocoa for some time but I feel that I need to know more about the AppKit framework. I'm often in doubt about which components I should use for specific uses. For example I've used much time researching how I should simply switch between two NSViews (in the same way as NSTabView but without its tabs and for example in an IBAction).
I've read Cocoa Programming for Mac OS X by Aaron Hillegass but I'm looking for what to read/do next to widen my knowledge about the AppKit framework and about developing user interfaces with Cocoa.
What do you recommend me to take a look at?
Thank you in advance.

Four or five years ago, I was asking the same question. I had just finished Hillegass' book as well, and wasn't sure where to look for "more."
To be honest, what worked for me was learning by necessity. Think of an idea for an application you'd like to write, and just write it. The problems that you encounter along the way will help you ask more specific questions, and you'll quickly build your skill set. There's rarely a be-all, end-all answer to a programming question. Experiment, and you'll find out what works, what doesn't, and how to work your way from the latter to the former.
If you find yourself asking questions like, "Well, I guess I still don't know how to do something like A...", then either you can search for tutorials/forums on how to accomplish A, find the preferred or official alternative to A, or maybe you didn't really need to do A in the first place. (And you're just asking questions for the sake of asking questions.)
If you're really hell-bent on learning everything, you could always just read through Apple's documentation, or examine the classes/discussion at CocoaDev: http://www.cocoadev.com/index.pl?AppKit

Learn by tinkering. AppKit is pretty straight-forward, and using it like it was intended is dead simple. When you're trying to use a class, the documentation usually contains a helpful tutorial, and everything you need to know about the class.
When you come across a control that doesn't exist, or isn't easily available, don't be afraid to subclass and make your own versions. There's also lots of open-source controls and UI code out there on developer blogs. As always, Google and StackOverflow are your friends.

I agree with all of the above, (and I am still asking the same question you are), but I will add one more resource, if you haven't already discovered it. If you look up an AppKit class, such as NSProgressIndicator, for example, in the Apple Documentation, there will often be sample projects supplied by Apple, and you can use them to discover how they used the AppKit class. Unfortunately, some of these haven't been kept up to date, but they are still useful.

Related

Requirements for learning Cocos2D?

I would like to make a 2D iPhone game, I've done some research and Cocos2D does seem like the best option, I am just wondering how much knowledge of Objective-C I will need before tackling Cocos2D. I know the fundamentals of programming in Objective-C - I can make a basic command line tool app. I've done some iOS SDK tutorials but I wouldn't know how to make an iOS app if I was asked because the tutorials I'm currently following are about three years old.
What is required as prior learning before using Cocos2D and does anyone know of any good resources? I'm more of a visual learner so I find videos more helpful than books or blog posts.
Thank you in advance for your time and any help :)
There is no required prior learning. Use it. If you get stuck on something, look it up, google it, or ask a question.
Meaning: don't waste your time learning general concepts without actually being in the situation of having to apply them.
Analogy: you don't need a PhD in mechanical engineering to drive a car - though it might help when you run into any issues. But in such a case just do what 99% of all people do: call for help. It hardly makes any sense to first learn the potential problems you can have with a car, you deal with them as they happen.
Also: frustration is part of the game and can not be avoided. :)
how much knowledge of Objective-C I will need
A lot. You need to master Objective-C before trying to make a bigger project like this.
And to understand Objective-C, you need to have a very solid knowledge of C.
(Also, for using 3rd-party frameworks lile Cocos2D, make sure you understand well the basic, default libraries such as Foundation and UIKit.)
For making games like Free flow , having an intermediate knowledge of Objective-C is sufficient. But if you plan on make something like Temple Run, then you need really good knowledge of Objective -C,Open-Gl and cocos2d..
The site I would recommend is raywenderlich.com, that's where i started from and they have tutorials from really basic level to really advanced level.
Hope this helps
The answer to that depends truly on who you are, your work ethics, and how much effort your are ready to consent to that effort. I would argue that if you have solid foundations with other OO language/frameworks, your quest is possible. I would start by 'repurposing' a freely available game ( you will find many good examples and tutorials from Ray Wenderlich and also Steffen Itterheim. Pick a sample project that is not too 'distant' from your game specification, and repurpose it. In doing so, you will break-in to objective-C and some of the basic frameworks you need to comprehend. Mastery will follow if you are dedicated and stick to it. Also, SO rules : many answers are available here : appropriate questions will yield many helpful hints and solutions to point problems.
ps. My biggest learning curve turned out to be xCode, a truly arcane and dogma-ladden IDE. I since switched to another IDE and my productivity has gone back to respectable levels.
I recently picked up Objective-C and cocos2d at the same time. I did a lot of reading about objective-c in the beginning to give me a primer. Aside from the syntax, most of that knowledge remains unused in my game. The most important things to learn about objective-c before starting in on cocos2d tutorials are, memory management, and debugging.
The reference count memory management might take a minute to get used to depending on what languages you are coming from. The general rule is, if you alloced/copied (without autorelease) or retained it, you must at some point release it, and if you need to use something beyond the current scope, you should retain it. You can see from some cocos2d examples of overwriting an objects dealloc method in order to dealloc anything you created/retained.
The debugging took me a minute to get used to as well. It helps if you add a high level try/catch block that you can use to log uncaught exceptions, otherwise some of your errors will give you little info to go on. You may also want to research/mess with some of your project settings to allow for better bug catching, such as enabling zombies. A big causer of crashes in objective-c is sending messages to already dealloced objects. Once you come across a badaccess error that you can't figure out, you will basically be forced to get knee deep in this stuff in order to figure out the cause.
Some other purely objective-c knowledge that will come in handy are getting very familiar with how NSArray and NSDictionary objects work, how they automatically retain/release, and how they can only hold objects (no primitive values). To work with floats/int and arrays/dictionaries, you need to convert primitives into something like an NSNumber object, which can be done fairly easily with a little research. Another helpful too is knowing how to save/load plists. There are convenience methods for doing this on NSArray and NSDictionary objects. This along with a little knowledge of the NSFileManager should be enough to help you save/load game states. If you want to get really fancy/clean in your coding, you will also learn about observers in objective-c. KVO can be great for making your UI update automatically on a certain objects property changes. For instance, my HUD KVOs the player object's life, so that when the life value changes, the HUD automatically updates. This can allow you to make a cleaner MVC type application. You can also register to listen to other types of messages (not just property changes), just make sure to unregister your listeners when you are done listening.
My last bit of advice would be to always pay attention to XCode warnings. If you are getting one, and you don't know why, you should figure out why. Some simple ones you might be able to ignore, others may cause bugs that you will not be able to track down any other way. For instance, I once used the function max() in my code, and xcode gave me a strange warning that I didn't understand. This caused havoc on my program by corrupting the stack. When I changed max() to MAX(), three or four unexplained bugs instantly disappeared. This kind of thing can set you back weeks, and take serious combing through every piece of your code if you don't catch it right away.
The rest can mostly be learned by looking at cocos2d code/examples. Good luck.

Fundamental Architecture of an iPhone App: what does exactly happen?

I am new to iPhone-Programming and I am probably asking a beginner's question, but I really couldn't find a satisfying answer to this:
I would like to understand the underlying architecture or structure of an iPhone App. What comes first, what are the parts that a starting template usually does automatically? In C-Programms, you know the program always jumps into main and then everything starts like you coded it. In iPhone-Programming, we start also in main and then something like the event loop starts and etc.there is a lot going on under hood, which I would like to understand so I feel more confident writing code and actually being aware of all consequences that my code causes.
I would be happy about any answers or are there any good references that are compact and only focussed on the coding architecture?
Read this for example ... http://www.technolike.com/archives/86/core-application-architecture-for-iphone.html ... and Google for more. There are many resources how to iPhone application starts, what's going under the hood, how event handling cycle does work, etc. Also when you read these resources, try to ask more specific questions. It's too broad question to answer everything.
I suggest that you browse through Apple's documentation on iOS development: http://developer.apple.com/library/ios/navigation/
If you want to learn the low level stuff first, perhaps you should look into how Objective C works and then move to learn about UIKit (which is the central framework/library for iOS app development).

How do you write good highly useful general purpose libraries?

I asked this question about Microsoft .NET Libraries and the complexity of its source code. From what I'm reading, writing general purpose libraries and writing applications can be two different things. When writing libraries, you have to think about the client who could literally be everyone (supposing I release the library for use in the general public).
What kind of practices or theories or techniques are useful when learning to write libraries? Where do you learn to write code like the one in the .NET library? This looks like a "black art" which I don't know too much about.
That's a pretty subjective question, but here's on objective answer. The Framework Design Guidelines book (be sure to get the 2nd edition) is a very good book about how to write effective class libraries. The content is very good and the often dissenting annotations are thought-provoking. Every shop should have a copy of this book available.
You definitely need to watch Josh Bloch in his presentation How to Design a Good API & Why it Matters (1h 9m long). He is a Java guru but library design and object orientation are universal.
One piece of advice often ignored by library authors is to internalize costs. If something is hard to do, the library should do it. Too often I've seen the authors of a library push something hard onto the consumers of the API rather than solving it themselves. Instead, look for the hardest things and make sure the library does them or at least makes them very easy.
I will be paraphrasing from Effective C++ by Scott Meyers, which I have found to be the best advice I got:
Adhere to the principle of least astonishment: strive to provide classes whose operators and functions have a natural syntax and an intuitive semantics. Preserve consistency with the behavior of the built-in types: when in doubt, do as the ints do.
Recognize that anything somebody can do, they will do. They'll throw exceptions, they'll assign objects to themselves, they'll use objects before giving them values, they'll give objects values and never use them, they'll give them huge values, they'll give them tiny values, they'll give them null values. In general, if it will compile, somebody will do it. As a result, make your classes easy to use correctly and hard to use incorrectly. Accept that clients will make mistakes, and design your classes so you can prevent, detect, or correct such errors.
Strive for portable code. It's not much harder to write portable programs than to write unportable ones, and only rarely will the difference in performance be significant enough to justify unportable constructs.
Even programs designed for custom hardware often end up being ported, because stock hardware generally achieves an equivalent level of performance within a few years. Writing portable code allows you to switch platforms easily, to enlarge your client base, and to brag about supporting open systems. It also makes it easier to recover if you bet wrong in the operating system sweepstakes.
Design your code so that when changes are necessary, the impact is localized. Encapsulate as much as you can; make implementation details private.
Edit: I just noticed I very nearly duplicated what cherouvim had posted; sorry about that! But turns out we're linking to different speeches by Bloch, even if the subject is exactly the same. (cherouvim linked to a December 2005 talk, I to January 2007 one.) Well, I'll leave this answer here — you're probably best off by watching both and seeing how his message and way of presenting it has evolved :)
FWIW, I'd like to point to this Google Tech Talk by Joshua Bloch, who is a greatly respected guy in the Java world, and someone who has given speeches and written extensively on API design. (Oh, and designed some exceptionally good general purpose libraries, like the Java Collections Framework!)
Joshua Bloch, Google Tech Talks, January 24, 2007:
"How To Design A Good API and Why it
Matters" (the video is about 1 hour long)
You can also read many of the same ideas in his article Bumper-Sticker API Design (but I still recommend watching the presentation!)
(Seeing you come from the .NET side, I hope you don't let his Java background get in the way too much :-) This really is not Java-specific for the most part.)
Edit: Here's another 1½ minute bit of wisdom by Josh Bloch on why writing libraries is hard, and why it's still worth putting effort in it (economies of scale) — in a response to a question wondering, basically, "how hard can it be". (Part of a presentation about the Google Collections library, which is also totally worth watching, but more Java-centric.)
Krzysztof Cwalina's blog is a good starting place. His book, Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries, is probably the definitive work for .NET library design best practices.
http://blogs.msdn.com/kcwalina/
The number one rule is to treat API design just like UI design: gather information about how your users really use your UI/API, what they find helpful and what gets in their way. Use that information to improve the design. Start with users who can put up with API churn and gradually stabilize the API as it matures.
I wrote a few notes about what I've learned about API design here: http://www.natpryce.com/articles/000732.html
I'd start looking more into design patterns. You'll probably not going to find much use for some of them, but as you get deeper into your library design the patterns will become more applicable. I'd also pick up a copy of NDepend - a great code measuring utility which may help you decouple things better. You can use .NET libraries as an example, but, personally, i don't find them to be great design examples mostly due to their complexities. Also, start looking at some open source projects to see how they're layered and structured.
A couple of separate points:
The .NET Framework isn't a class library. It's a Framework. It's a set of types meant to not only provide functionality, but to be extended by your own code. For instance, it does provide you with the Stream abstract class, and with concrete implementations like the NetworkStream class, but it also provides you the WebRequest class and the means to extend it, so that WebRequest.Create("myschema://host/more") can produce an instance of your own class deriving from WebRequest, which can have its own GetResponse method returning its own class derived from WebResponse, such that calling GetResponseStream will return your own class derived from Stream!
And your callers will not need to know this is going on behind the scenes!
A separate point is that for most developers, creating a reusable library is not, and should not be the goal. The goal should be to write the code necessary to meet requirements. In the process, reusable code may be found. In that case, it should be refactored out into a separate library, where it can be reused in the future.
I go further than that (when permitted). I will usually wait until I find two pieces of code that actually do the same thing, or which overlap. Presumably both pieces of code have passed all their unit tests. I will then factor out the common code into a separate class library and run all the unit tests again. Assuming that they still pass, I've begun the creation of some reusable code that works (since the unit tests still pass).
This is in contrast to a lesson I learned in school, when the result of an entire project was a beautiful reusable library - with no code to reuse it.
(Of course, I'm sure it would have worked if any code had used it...)

Learning Cocoa. Should I delve into Apple's documentation?

I have built some basic apps for iPhone and Mac with a general understanding of Cocoa, but haven't had a detailed understanding. Now I want to go deeper.
I have just finished the Objective-C documentation at MacDevCenter. It was great to get in-depth look but took far too much time, specially because a lot of it is conceptual, and it's hard to build detailed examples to make use of the concepts.
Now I am on to Cocoa, but feel like it would be too much work to go through 250 page documents for Cocoa itself, then KVC, Cocoa Bindings, and Core Data.
Would I be better off at this stage picking up a good book on Cocoa (Hillegass' is too sparse I think) or should I just bite the bullet and go through the docs?
Apple's documentation is generally excellent. The API reference in the very least is some of the best I've read (right up there with Java's API reference), and is a very valuable asset.
I'd suggest reading the overview pages for topics you're interested in to get a general "feel" for how they work and a starting point, and then just start writing code. You can refer to the documentation to fill in blanks that you encounter while writing code.
Writing a small project to exercise your use of APIs you'd like to learn is a great way to get started.
I've found Hillegass's book to be far from sparse. In fact, I've found it to be one of the most dense books on programming I've read. I've re-read most of the chapters at least 4 times, and each time understood more. By re-reading, I've understood the reason he included a certain paragraph and how it fits with the bigger picture. He spells everything out very clearly and doesn't waste words. I'm amazed at the amount that's contained in that book.
You mentioned that you want to learn by doing, instead of by reading. The Hillegass book has a section at the end of each chapter where you put what you've learned to the test, by building an application.
Of the three things you talked about, KVC, Cocoa Bindings and Core Data, the first two are covered very well. I did find the Core Data chapters to be a lot more sparse. That's the only part of the book that I haven't been able to understand without other documentation. The Pragmatic Programmers book covers it much better.
Anyway, the answer to your question is: it depends. If you've managed to work your way through the Mac Dev Center documentation, it may be that you're the kind of person who learns easily from that style of resource. If you like learning from books, the best one is Hillegass.
I'd recommend an excellent book released some time ago: ‘Cocoa Design Patterns’ by Erik M. Buck and Donald A. Yacktman.
Of course you can dive into Apple's documentation, but to me, I found Hillegass' book is a good start for learning Cocoa. The value of the book, I think, is the introduction of the conventions and idioms of Cocoa.
Is it really clear for a new Cocoa developer to find out information on issues like when and when not to send message autorelease to an object? You can argue that a single Googling will do, but how do you know there are these kind of issues in the first place?
Go get a good book, look through it and take notes. It could save your time. For me, on Cocoa, the book is Hillegass'.
Hillegass' book will drive you to the free documentation every time he fails to make sense in order to be "funny" (i.e. every other paragraph) anyway, so unless you just want to give him $40, you will be much better off sticking to what Apple gives you au gratis. At the very least you won't pick up any of his horrible UI design habits.
You don't really need to go through ALL the bindings and Core Data docs page by page--half of it is stuff you'll likely never use. Knowing where to look for more information is far more valuable a skill than memorizing APIs.
The reference is great and invaluable, but might not be the easiest way to start coding (as people have mentioned).
I started looking at the stanford lectures on iphone development, then coded as I went along. I've actually used them as reference once or twice as well... They are available on itunes for free.
It depends on what other programming experience you have and how you learn best. If you're fairly new to programming in general, I'd recommend Hillegass to start. If you're fairly competent at OOP but don't know Objective-C, you can go for Apple's "Introduction to The Objective-C Programming Language" to learn ObjC's language features. If you've programmed using paradigms other than OOP, you could look for general OOP book recommendations here on SO (e.g. "Best Referece to learn OOP independent of languages?"). I also second Jonatan's recommendation to check out what programming videos iTunes University offers. They are recordings of lectures from top-tier universities and generally of good scholastic quality (I only qualify this with "generally" because I haven't seen them all; the ones I have seen invariably were of quality). No matter where you go to learn ObjC and Cocoa, you'll still yourself using Apple's docs as reference.
As you learn, you might also find CocoaDev a useful ancillary source.
Aaron Hillegass's book is the text for his one-week cocoa development seminar, and it has been revised over several years of teaching Cocoa to beginners. Aaron's course is where Apple sends their own employees, now that they don't do the Cocoa course in-house anymore.
Apple's documentation is an excellent reference, but I agree that it's a huge resource to try to learn Cocoa from in the first place. Besides, you probably don't need to know the whole API up front -- just learn each part as you need it.
When I started learning, I just picked a project, sat down and started trying to code. When I ran into something I didn't know, I just googled it, which led to a tutorial and/or the relevant page of Apple's documentation.
In my experience, the best way to learn something like this is, well, experience.
Apple's documentation generally contains excellent API references, but (in my opinion) lacks equivalent topical guides. This makes it a bit hard to follow for beginners (Jacob Kaplan-Moss in the link I just gave makes the point that Python's documentation is like this, but I think Apple's is much more).
My recommendation would be to pick up Hillegass's book, alongside a project. The first few chapters explain fundamentals, so make sure to read those (and do the exercises!), or you won't be able to get far. Then try and implement your project idea; when you reach a wall, look through the Table of Contents and the glossary of the book and see if there's something that covers it. If you find something, use this as an excuse to read it fully and understand it. When you're finished, you'll have both a greater understanding and a project to show for it. Furthermore, you'll know what to search for in Apple's API references next time you try to implement something. This helped motivate me to finish the book, maybe it'll be helpful for you.[1]
[1]: (disclaimer: I have an odd learning style)

Open source Objective-C projects with high quality code?

I think one of the best ways to learn a new programming language is to dive into source code and see how experienced coders write. I've found this kind of question asked here on Stack Overflow for other languages, but none targeted at Objective-C. The closest I've found is this question about good-looking Cocoa user interfaces, but I'm thinking in terms of general code quality, such as good use of idioms and design patterns, and usable documentation. Another good characteristic would be a code-base that is large enough to require real organizational discipline, but small enough that a beginner can fit his/her head around.
What do you think is a high-quality open source project that meets these criteria?
There's a decent list of open source Mac projects on CocoaDev: http://cocoadev.com/CocoaOpen
Not all of the projects are still active, but a decent number of them are. I particularly recommend:
BWToolkit - http://bitbucket.org/bwalkin/bwtoolkit/
BGHUDAppKit - http://www.binarymethod.com/bghudappkit/
CHDataStructures - http://cocoaheads.byu.edu/code/CHDataStructures
MGTwitterEngine - http://mattgemmell.com/source
Sparkle - http://code.google.com/p/sparkleplus/
Colloquy - http://colloquy.info/project/wiki/Source%20Code
CocoaRest - http://github.com/sdegutis/CocoaREST
CorePlot - http://code.google.com/p/core-plot/
I've found Adium to be a well-designed and written Objective-C project. It has a huge codebase, too.
I don't think there's any one place to learn all the Objective-C idioms and best practices, but you can pick them up here and there over time and practice. Cocoa mailing lists and blogs are good resources for this. (There are several SO questions relating to that.)
I haven't dived into the source myself, but the Omni frameworks are well-respected in the Mac community.
I work on a framework of my own that's still rough around the edges, but I strive for quality documentation in addition to understandable, well-structured code.
Google Quick Search Box [ qsb-mac - Google Code ]
I think The Google Quick Search Box (QSB) is worth a look. IMHO it's pretty much a cleaner googley rewrite of Quicksilver (Note: N. Jitkoff, the QS programmer, is also involved in the Google QSB).
Apple has tons of downloadable examples on their developer website. You get to see the source, and the license is BSDish from what I recall (use it for whatever, but don't repost original code without the license).
http://developer.apple.com/samplecode/Cocoa/index-date.html
Check Out lots off open source project here.
http://code4app.net
http://code4app.com
https://www.cocoacontrols.com
Here's a test app I wrote earlier today. Download links under “get source” in the top-right.
It's small, but it demonstrates good organizational discipline (separate controller object, instead of everything crammed into AppDelegate), a simple model layer with one model class (Header), and correct use of Cocoa Bindings.
I used to work on the GNUstep sources, they've got a good consistent coding style and the code is well-organised. Of course the project is hardly small, but given that you know what most of the classes do as you've used them in your Cocoa projects it's easy to zoom in on a piece that interests you. Some of the GNUstep Applications Project or Étoilé apps may also be a good place to start.
Check out PSTreeGraph, a view control for iPad applications.
iMedia Browser
, shows support of 10.4 - 10.7 strategies, is neatly and purposefully organized, and deeply commented. inherent problems using ImageKit have been boldly approached and solved.
i aspire to this level of thought mixed with practicality.