Cocoa Objective-C on Mac OS X
This website is about writing native software for Apple Computer's upcoming Mac OS X operating system using the Cocoa framework and the Objective-C language.
Contents
- July 10 2000: Why ?
- July 12 2000: Where to find more information ?
- July 15 2000: Calculator
- July 20 2000: Edit
Apple is currently selling one million computers each quarter. Soon the installed base of millions of Macintoshes will be switching to Mac OS X. This breakthrough operating system will combine the core features of Unix-like operating systems - proven rock solid stability and top notch performance - with what makes Macintosh computers so unique - their overall quality, their superb software-hardware integration, their advanced features and above all their focus on the user experience.
Mac OS X will be able to run existing Mac OS applications in its Classic environment but without most of the advantages of Mac OS X. Existing Mac OS application should be rewritten to use the Carbon API to become native Mac OS X applications. Carbon is a classic procedural API based on the original Mac OS toolbox. Although it is a great solution for porting existing Mac OS applications, there is a better alternative for new applications.
Mac OS X includes a unique advanced object oriented framework called Cocoa. Cocoa is unique in that it allows you to write native Mac OS X consumer applications that are fast, great looking, powerful, platform comformant and integrated. Best of all, from a software developer's viewpoint, Cocoa delivers on the promise of object technology: once past the learning curve, you are doing more with less effort, reusing proven features of the framework.
The Cocoa framework can be accessed using two languages: Objective-C and Java. Although I am using Java almost exclusively in my day job, I think Objective-C is the preferred language to write native Cocoa Mac OS X consumer applications. Objective-C is the implementation language of Cocoa framework itself and gives you access to all its features with a faster startup time as well as a better overall performance - both in execution speed as well as memory usage.
Since there isn't a lot of information on getting started with Cocoa on Mac OS X Developer Preview 4, I decided to start this website. I want to share my experiences, learn from others and help others get started.
The larger the Cocoa developer community, the more native software we will get for Mac OS X. The more native software for Mac OS X, the more successful Mac OS X will be. The more successful Mac OS X is, the larger the market for software developers using Cocoa.
July 12 2000: Where to find more information ?
Although Mac OS X is currently in Developer Preview 4 release and distributed under a non-disclosure agreement, there is public information available. The general consensus is that discussing the Cocoa API is allowed while discussing Mac OS X performance and features is not. This is a set of links that should get you started:
- Apple's Mac OS X preview site
- Apple's Mac OS X developer site with documentation on Cocoa and Objective-C PDF manual
- Apple's Darwin site, the open source core of Mac OS X, including the Darwin-Development and DarwinOS-Users mailing lists
- the Stepwise community site
- the community section operated by Omnigroup, with the excellent MacOSX-dev, MacOSX-talk and MacOSX-admin mailing lists and source code
- check out these companies with commercial Mac OS X DP4 software: Omnigroup, Stone Design, FrontBase, CaffeineSoftware, OpenBase
I have been using a Calculator example for years, from development environment to development environment. It was only natural that this became my first experiment. It took me only a couple of short programming sessions to get results. I must admit that I more or less knew Objective-C and Cocoa from years ago when I took an Apple Developer class on the subject (then called Openstep on Rhapsody) - however I never continued on that trail. I also reread the book 'DISCOVERING OPENSTEP: A Developer Tutorial (release 4.1)' which I had.
My Cocoa Objective-C calculator looks (I borrowed the icon) and feels (its faster actually) just like the standard calculator shipping with DP4. Furthermore, it sports full unlimted undo and redo. I asked two technical questions on Omnigroup's MacOSX-dev list.
Q. How to make a non-document based application quit when its sole window was closed?
A. Let your application's delegate implement
- (BOOL)applicationShouldTerminateAfterLastWindowClosed: { return YES; }Thanks to oloft@cs.chalmers.se (Olof Torgersson) and to Bresink@t-online.de (Marcel Bresink)) for answering this one.
Q. How to implement cut/copy/paste yourself, or how to put something on the pasteboard or get something from the pasteboard?
A. I ended up doing it like this:
- (void)copy:(id)sender { NSString *contents = // from somewhere NSPasteboard *pasteboard = [NSPasteboard generalPasteboard]; [pasteboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; [pasteboard setString:contents forType:NSStringPboardType]; } - (void)paste:(id)sender { NSPasteboard *pasteboard = [NSPasteboard generalPasteboard]; NSString *type = [pasteboard availableTypeFromArray:[NSArray arrayWithObject:NSStringPboardType]]; if (type != nil) { NSString *contents = [pasteboard stringForType:type]; if (contents != nil) { // use somewhere } } }Thanks to snoyes@Bix.Com and stark@easynet.fr (Frederic Stark) for the answer.
You can download both the binary (in case you want a calculator replacement with full undo/redo capabilities), or the source code (for Mac OS X DP4, new project builder). I welcome all feedback.
- Calculator.app.tar.gz (binary, 35 Kb)
- Calculator.tar.gz (source, 17 Kb)
While a calculator has a tool-like GUI with one single window, most applications are document-based. The Mac OS X DP4 tools (the new project builder and interface builder) as well as the Cocoa libraries offer exceptional support to get a simple document-based application up and running.
I tested this by trying to build a simple ASCII text editor, called Edit. All in all I wrote (or edited) about 10 lines of code. Making my application do only ASCII (RTF is the default text format of Cocoa) took some effort. Most of the work went into the new project builder's target application settings properties list. I looked a lot to what was done in the examples TextEdit and Sketch to figure things out - once again not all documentation is up to date (i.e. DP4, new project builder, new packaging requirements).
You can download both the binary (in case you want a TextEdit replacement that can only do ASCII), or the source code (for Mac OS X DP4, new project builder). Again, I welcome all feedback.
- Edit.app.tar.gz (binary, 40 Kb)
- Edit.tar.gz (source, 36 Kb)
Copyright © 2000 Sven Van Caekenberghe. All Rights Reserved.