ADC May 2001 4/6/01 2:27 PM Page 1

Apple Developer Connection Direct

Cocoa: A New Flavor to Mac OS Development

ac OS X integrates five separate application runtime environ- imaging and printing model, multimedia standards QuickTime and ments—Carbon, Cocoa, Classic, Java, and BSD—into one seam- OpenGL, and Internet and BSD services, too. Localization and Mless whole, providing developers with many options. This arti- Internationalization are also well supported by Cocoa. The separation of cle introduces Cocoa, an important development technology used by user interface elements from executable code allows you to package your Apple, to develop many of the applications that ship with Mac OS X. applications for different locales easily, with no code changes. All Cocoa Cocoa constitutes a new, highly efficient way for developers to create new text drawing utilizes the Unicode standards. The text and font systems are products for Mac OS X. particularly flexible and allow you to use sophisticated word processing features with little effort. A Brief Overview An important development advantage that Cocoa offers is the capability Cocoa is an object-oriented application framework and runtime environ- to develop programs quickly and easily by assembling reusable compo- ment—a set of software components used to construct applications that nents. With reusable components, developers can not only create applica- run on Mac OS X. Think of Cocoa as a large set of reusable application tions, but also produce: building blocks that can be used as delivered or • Frameworks (sophisticated library structures) extended for your specific needs. • Bundles of executable code and associated resources which can be Cocoa is based on OpenStep, an object-ori- loaded and executed dynamically ented technology that was originally intro- • Collections of custom user-interface objects duced in 1987 as NeXTSTEP and has been These capabilities support the easy creation and distribution of applica- refined through many iterations. tion plug-ins and extensions. Consequently, Cocoa is mature technology based on a design years ahead of other Tools and Resources object-oriented frameworks. Apple put the tools for Cocoa development directly into the hands of Like any software library, Cocoa has a developers by including them in every Mac OS X package on the Mac OS X learning curve for newcomers, but it is not Developer Tools CD. Just install the Developer.pkg file and the complete an overly difficult one and productivity comes Apple tool suite is installed and ready for use. This installation includes quickly. The Cocoa framework delivers a great deal of fundamental appli- the Project Builder Integrated Development Environment, Interface cation functionality so you can spend the majority of your energy working Builder—the application for designing and testing user interfaces and on application features rather than managing the more tedious parts of establishing the connections between objects and actions—as well as all system interface and user experience implementation. the interface files, debugging and performance tools, and full online docu- The primary implementation language used in Cocoa is Objective-C, a mentation. superset of ANSI C with specific language features added to allow for Once you have installed the Developer.pkg, open a window and object-oriented programming. The language extensions in Objective-C are you will find the /Developer directory on your system volume. Inside this compact and easy to learn. Cocoa applications make extensive use of the directory, in /Applications, are the Project Builder and classes and methods in Cocoa component libraries. However, because of applications along with many other tools. In /Developer/Documentation Objective-C’s compatibility with ANSI C, they can also make use of core are folders containing PDF and HTML help and documentation files for functionality contained in traditional C and C++ libraries brought from Cocoa and the developer tools. other application environments. Finally, in /Developer/Examples are /AppKit and /Foundation sample code folders with Cocoa applications for you to learn from and even reuse Features and Development Scope for your own project. Cocoa is a peer to the Carbon and Java application development environ- ments in Mac OS X. Cocoa supports all Mac OS X application service fea- Taking Apart the Technology tures. For example, Cocoa applications can access the Mac OS X native Cocoa is comprised of two object-oriented frameworks: Foundation

Godfrey DiGiorgi is Technology Manager for Development Tools & Cocoa in Apple Worldwide Developer Relations. He’s been associated with Macintosh development for more years than he’d care to admit. He can be reached at [email protected]. ADC May 2001 4/6/01 2:27 PM Page 2

Updates from the Apple Developer Connection May 2001

(Foundation.framework) and Application Kit (AppKit.framework). The can do so easily through the use of a mechanism called inheritance. You Foundation classes provide the low-level objects and functionality that only need to code the specific functionality you are adding to a class, the form the basis of the Cocoa environment. The classes in Application Kit rest of the object’s behavior is inherited from the preexisting parent class. provide the functionality users see in the user interface, which respond to For more information, see the documentation in: system events such as mouse clicks and key presses. The Application Kit is /Developer/Documentation/Cocoa/ObjectiveC/index.html. layered directly on Foundation. Here is a brief look at the functionality contained in each of these frameworks. Fundamentals of Cocoa Programming The Foundation Framework is designed to provide a set of basic utility Cocoa is a rich object-oriented environment. Object-oriented programming classes, introduce consistent conventions for paradigms (such as memory makes heavy use of patterns to simplify design and implementation of com- management) support Unicode strings, object persistence, and file man- plex systems. The three most essential patterns to learn when starting are: agement. Foundation includes: 1. Model-View-Controller (MVC) - MVC defines three types of objects in • the root object class an application: model, view, and controller. Model objects hold data and • classes representing basic data types such as strings and byte arrays define the logic that manipulates that data. View objects represent user • collection classes for storing other objects interface elements (a window, for example). Controller objects act as • classes representing system information such as dates and mediators between model objects and view objects. This mediation role • classes representing communication ports allows view objects to be free from the programmatic interfaces of models Several paradigms are also defined in Foundation to help avoid confu- and vice-versa. sion in common situations and introduce consistency across class hierar- 2. Target/Action - This is part of the mechanism by which user interface chies. This is done with some standard policies, like the one used for controls respond to user actions. When a user clicks a user interface con- object ownership (answering questions such as: “Who is responsible for trol, the control sends an action message to the target object. disposing of an object?”), and also with abstract classes which enumerate 3. Delegation - Delegation lets you modify an object’s behavior without cre- over collections. These paradigms reduce special and exceptional cases in ating a custom subclass. A delegate acts on behalf of another object. When code management and allow reuse of the same mechanisms with various a delegate receives a message (from a window, a view, etc.), the sender of kinds of objects. All together, these paradigms improve development effi- the message is allowing the delegate to influence its behavior and aid in ciency and productivity. decision-making (such as: “Should I allow the user to close me?”). The Application Kit framework contains all the objects needed to Cocoa leverages the dynamic binding and object introspection capabil- implement the graphical, event-driven user interface: windows, panels, ities of Objective-C and Java by allowing delegate objects to implement buttons, menus, scrollers, text fields, etc. The Application Kit handles all just the functionality they want to influence. At runtime, the delegating the details for you as it efficiently draws on the screen, communicates with objects can query their delegates to see what methods have actually been hardware devices and screen buffers, clears areas of the screen before implemented. This saves you from having to subclass some specific par- drawing, and clips views. There are over a hundred classes in the ent class (that has all the default implementations), helping to preserve Application Kit, so it might seem a steep learning curve, but many of the your application’s unique class hierarchy. Application Kit classes are support classes that are only used indirectly. For a detailed listing of the Foundation and Application Kit object Continued on page ?? ☛ classes, see the documentation in: /Developer/Documentation/Cocoa/CocoaTopics.html.

Object-Oriented Programming Project Builder For traditional Mac programmers, Cocoa development represents a para- digm shift—from a procedural to an object-oriented development model. Tips & Tricks The “free” functionality found in the Foundation and Application Kit • You can use Build Styles instead of duplicate targets frameworks helps Mac programmers realize the benefits of this easy, natu- for many things that would require duplicate targets ral way to develop applications. in other environments. Object-oriented programming allows the construction of complex • All text fields in Project Builder that contain file paths applications through assembling small, well-tested, reusable modules support path completion (completion is bound to called objects. This provides three simple advantages: the F5 key by default). 1. Greater reliability by breaking complex implementations into smaller, • Any place where single clicking an item loads that easily testable components. item into the built-in editor, double-clicking will open 2. Easier maintainability due to the small, modular nature of objects. (This small size allows one to fix bugs found in testing more easily.) the item in a separate window. This includes the 3. Greater productivity through reuse. The ability to use an existing class files list, bookmarks, targets, build results, find over and over again means less redundant work. When you need to results, etc. extend the functionality of a particular class to meet a specific need, you ADC May 2001 4/6/01 2:27 PM Page 3

QA1019 - Can’t attach during two-machine debugging with GDB QA1018 - Using AppleScript to send an email with an attachment New Mac OS X QA1013 - Mac OS X and root access Related Releases SC - More than thirty new Mac OS X code samples were posted to the ADC web site since the last issue. Please visit the URL below for a complete list of sample code. http://developer.apple.com/samplecode/ The following software is available from the Download Software area of the ADC Member Site at: http://connect.apple.com/

• CarbonLib 1.3d9 SDK Upcoming Seminars The latest prerelease version of the CarbonLib 1.3 SDK for Mac OS • Mac OS USB Driver Development Kit 1.5 and Events http://developer.apple.com/hardware/usb/download.htm For more information on Apple developer events please • FireWire 2.8.1 Software Developers Kit (SDK) visit the developer Events page at: http://developer.apple.com/hardware/FireWire/Developer_Info.html http://developer.apple.com/events/ #FireWireSDK

Developer Documentation Training and Seminars • O'Reilly and Apple Collaborate on Mac OS X Book Series • Apple iServices: Cocoa Development Classes O'Reilly & Associates, has announced plans to publish a series of This five-day course provides comprehensive, hands-on training books about Mac OS X development. The books in this series have using real-world examples. With the skills acquired in this been technically reviewed by Apple engineers and are recommend- ed by the Apple Developer Connection. The first Mac OS X titles, course, developers can build full-featured applications using the Learning Carbon and Learning Cocoa, are available this May. In most advanced software environment on Mac OS X. addition, the O'Reilly Network has established the Mac DevCenter, a http://www.apple.com/iservices/technicaltraining/ web forum for development news and articles. cocoadev.html • Apple Technical Publications • Programming With Cocoa Over thirty new and updated documents have been added in the Taught by Aaron Hillegass at the Big Nerd Ranch, Ashville, NC last month to help developers with successful Mac OS X application and Atlanta, GA. Five-day classes are taught on developing and peripheral development at: web-based and Mac OS X applications. http://developer.apple.com/techpubs/ http://www.bignerdranch.com/when.html

TN2015 - Locating Application Support Files Under Mac OS X Developer Related Conferences TN2014 - Insights on OpenGL TN2013 - The 'plst' Resource •Worldwide Developers Conference (WWDC) 2001, TN2012 - Building QuickTime Components for Mac OS X San Jose, CA May 21-25 Register now for Apple’s Worldwide Developers Conference “Built for Mac OS X” 2001, which takes place in San Jose, California from May 21-25. ADC Premier members receive one free pass to the conference. Artwork Now Available For schedules and other details check out: Now that customers have Mac OS X in their hands, http://www.apple.com/developer/wwdc2001/ they’ll be looking for great products to run on it. Tell the world that your product runs on Mac OS X by displaying • MacHack Conference, Dearborn, MI the “Built for Mac OS X” badge on your product’s pack- June 21-23 aging. The artwork, licensing requirements, and usage MacHack, in its sixteenth year, remains centered around cutting guidelines are available on the ADC Software Licensing edge software development. MacHack’s uniqueness derives from web site. the informal feel and the LIVE coding that occurs around-the- http://developer.apple.com/mkt/swl/agreements. clock during the conference. html#macosx http://www.machack.com/ ADC May 2001 4/6/01 2:27 PM Page 4

Updates from the Apple Developer Connection May 2001

Cocoa: A New Flavor to Mac OS Development Continued from page ??

These patterns are discussed at length in Inside Cocoa: Object- Did You Know? Oriented Programming and the Objective C-Language and in other parts of the Cocoa documentation. Learning Cocoa—and Mastering It Another integral part of Cocoa programming practice is the use of o programmers new to Cocoa--Apple’s power- Interface Builder. Interface Builder is a design tool, allowing you to easily ful object-oriented application environment-- define and test a user interface. It is also used with Cocoa to define object classes and “wire” the connections of targets and actions. Interface Builder T the road to mastery is a challenging yet creates ‘nib’ files, which are a static representation of objects and their rewarding one. Although there are many new things to relationships. These nib files are efficiently loaded as needed at runtime. learn, once you become comfortable with Cocoa, your Interface Builder is closely tied to the Project Builder IDE for a smoothly programming productivity will take off. Guaranteed. integrated development experience. For more information on Interface To help you in your Cocoa apprenticeship, Apple Builder and Project Builder, see the documentation available in: /Developer/Documentation/DeveloperTools provides two great sources of technical information. as well as on the Web at: The first is the book Learning Cocoa, published by http://developer.apple.com/tools/projectbuilder/ O'Reilly. Written by insiders at Apple Computer, http://developer.apple.com/tools/interfacebuilder/ Learning Cocoa mixes conceptual overviews with Language Support hands-on tutorials to give you a crash course in Cocoa is implemented in Objective-C. As a superset of ANSI C with special Cocoa application development. The idea is that syntax and runtime extensions, Objective-C lets you use object-oriented learning Cocoa should not be just a matter of reading, programming techniques while leveraging as much of the use and knowl- but doing. Learning Cocoa guides you through the edge of standard ANSI C as possible. creation of several applications, each more complex Java can also be used to implement Cocoa applications through the use of the Java API versions of the Foundation and Application Kit frameworks. than the one before. By the end of the book, you'll be See the Foundation and Application Kit reference locations. prepared to take on serious application development on your own. Look forLearning Cocoa in technical Where to Go for More Information bookstores near you; you can also purchase it direct • Start with the Cocoa technology web page. It includes News and from O'Reilly at: Updates as well as a list of Resources for Cocoa development. http://www.oreilly.com/catalog/learncocoa/ http://developer.apple.com/cocoa/ • Read Inside Cocoa: Object-Oriented Programming and the The second source of information on Cocoa is Objective-C Language. Apple’s own technical publications, especially its http://developer.apple.com/techpubs/macosx/Cocoa/CocoaTopics.html Cocoa programming topics. The programming topics http://www1.fatbrain.com/documentation/apple/ are a hierarchically organized collection of information • Sign up for one of Apple’s development-oriented mail lists, such as Cocoa Developer, ProjectBuilder-User, or Java Developer. nodes on such topics as implementing undo, custom http://lists.apple.com drawing, and managing text. Each node brings togeth- • Check out the mailing lists for Mac OS X and Objective- C developers er (in an HTML frame set) the conceptual, procedural, supported by the Omni Group. and reference documentation that illuminates a single http://www.omnigroup.com/ programming task. In addition to documentation, a • Look for the O’Reilly book, Learning Cocoa, due in May, 2001. http://www.oreilly.com/catalog/learncocoa programming topic includes links to example projects, technical notes, and other sources of information. If In Summary you have the Developer package installed, you can The demand for Mac OS X applications is huge and Cocoa can help you access the Cocoa programming topics through the bring new products to market quickly, using the full power of Mac OS X Developer Help Center. You can also view them on the development tools and object-oriented methodology to facilitate your work. Whatever Mac OS X development path you choose, Apple is eager to ADC Developer Documentation web site at: assist you. For the latest Mac OS X news and information, visit the Apple http://developer.apple.com/techpubs/macosx/Coc Developer Connection web site at: oa/CocoaTopics.html http://developer.apple.com/macosx