210 likes | 363 Views
Mac OS X Programming. Using Cocoa with Objective-C. © 2003 Greg Howell (greg@diversionary.net). What is covered:. Cocoa application development Objective-C as the primary programming language Jaguar (10.2) Developer Tools although most comments apply to Xcode (Panther). What is not covered:.
E N D
Mac OS X Programming Using Cocoa with Objective-C © 2003 Greg Howell (greg@diversionary.net)
What is covered: • Cocoa application development • Objective-C as the primary programming language • Jaguar (10.2) Developer Tools although most comments apply to Xcode (Panther)
What is not covered: • Carbon application programming • Porting Unix applications to Mac OS X • Java programming
The Apple Developer Tools • Project Builder (or Xcode under Mac OS X 10.3) • IDE (code editor and project browser) • Front end for GCC • Interface Builder • Used for GUI construction • Used to link UI elements to methods in code • Both tools have a significant NeXT heritage
Project Builder & Xcode • Used to edit source code • Edit source code • Set project properties • Set compilation options • Set properties of compiled application
Interface Builder • Used to edit the .nib file • Can access this through Project Builder or Xcode • The .nib file defines what objects exist at startup in the application • The .nib file specifies the links between user interface elements and methods in objects
Cocoa • Set of OO APIs derived from NeXT technologies • The NS prefix of most Cocoa objects stands for NeXTSTEP (seems to have followed Mr. Jobs) • Framework for building applications • Set of objects that mediate between the program and the OS • Used to take advantage of Carbon features • Most of the iApps are built on top of Cocoa • iPhoto, iDVD, iChat, Mail
Objective-C Programming • General syntax • .m and .h files • General project structure • How a Cocoa application starts up • Using other languages
Objective-C • An extension of ANSI C • Programmer can choose to implement features as classes (in an OO manner) or use standard C methods such as structures • Main extensions are message passing and the syntax for defining classes and methods • The extensions are straightforward, the OO design principles are sometimes hard to comprehend
Objective-C: General Syntax anObject = [NSObject alloc]; Returned Value Message Receiver Method Name
Objective-C: .m & .h files • .m files • Objective-C implementation file for a class • .h files • Header files for .m files • Define the interface to the class • Can also use .h files as standard C style header file for macros, declarations etc.
Objective-C: Project Structure • main.m • Contains the main() function • Objective-C class and interface files • Usually one application controller class • Other classes as required • Other C header files (if required) • Other C source files (if required)
Objective-C: Typical Start Up • main() function in main.m calls NSApplication() • This opens the .nib file for the application • The .nib file defines what objects are created • The classes which define these objects can have a startup method (called awakeFromNib())
Objective-C & Other Languages • Just add the functions into the .m implementation file and call it wherever it is required • I used a Pascal call-back routine in the screen reader to operate TTS
Sources of Documentation • General programming websites • Apple’s Developer website (in particular the sample code section) • Books (O’Reilly and others) • Documentation on hard drive installed with Developer tools (both documentation and sample code)
Strings • There are two types of strings I encountered: • Cocoa strings (string objects) • “C” style strings (character arrays) • Cocoa String Objects • NSString (standard string object) • NSMutableString (string object that is able to be manipulated) • Straightfoward to convert between the two
Arrays • NSArray object
Speech Synthesis • Need to create a new speech channel • If you use the standard OS speech channel you can generate speech but you have no control over it • You can specify TTS properties when you create a speech channel (or just use the system defaults) • Once created it is easy to generate speech • The TTS engine runs as a separate process (leaving the application to do what it wants) • It is interruptible (no queue) • You need a call-back routine to determine when the TTS engine finishes generating speech from the string you sent it
Putting Together an Application • Adding an icon to the application • About Box tricks • Can use a text, RTF or image file • Changing version numbers • There are two places to alter version numbers • Distributing an application • Disk image (.dmg file) • Installer package