You are previewing Learning Cocoa with Objective-C, 2nd Edition.
O'Reilly logo
Learning Cocoa with Objective-C, 2nd Edition

Book Description

Learning Cocoa with Objective-C is the "must-have" book for people who want to develop applications for Mac OS X, and is the only book approved and reviewed by Apple engineers. Based on the Jaguar release of Mac OS X 10.2, this edition of Learning Cocoa includes examples that use the Address Book and Universal Access APIs. Also included is a handy quick reference card, charting Cocoa's Foundation and AppKit frameworks, along with an Appendix that includes a listing of resources essential to any Cocoa developer--beginning or advanced. Completely revised and updated, this 2nd edition begins with some simple examples to familiarize you with the basic elements of Cocoa programming as well Apple's Developer Tools, including Project Builder and Interface Builder. After introducing you to Project Builder and Interface Builder, it brings you quickly up to speed on the concepts of object-oriented programming with Objective-C, the language of choice for building Cocoa applications. From there, each chapter presents a different sample program for you to build, with easy to follow, step-by-step instructions to teach you the fundamentals of Cocoa programming. The techniques you will learn in each chapter lay the foundation for more advanced techniques and concepts presented in later chapters. You'll learn how to:

  • Effectively use Apple's suite of Developer Tools, including Project Builder and Interface Builder

  • Build single- and multiple-window document-based applications

  • Manipulate text data using Cocoa's text handling capabilities

  • Draw with Cocoa

  • Add scripting functionality to your applications

  • Localize your application for multiple language support

  • Polish off your application by adding an icon for use in the Dock, provide Help, and package your program for distribution

Each chapter ends with a series of Examples, challenging you to test your newly-learned skills by tweaking the application you've just built, or to go back to an earlier example and add to it some new functionality. Solutions are provided in the Appendix, but you're encouraged to learn by trying. Extensive programming experience is not required to complete the examples in the book, though experience with the C programming language will be helpful. If you are familiar with an object-oriented programming language such as Java or Smalltalk, you will rapidly come up to speed with the Objective-C language. Otherwise, basic object-oriented and language concepts are covered where needed.

Table of Contents

  1. Learning Cocoa with Objective-C
  2. A Note Regarding Supplemental Files
  3. Preface
    1. Audience
      1. Who Should Read This Book
      2. Who Should Not Read This Book
      3. What You Need to Know
    2. About the Example Code
    3. How This Book Is Organized
      1. Part I
      2. Part II
      3. Part III
      4. Part IV
      5. Part V
    4. How to Use This Book
    5. Conventions Used in This Book
    6. How to Contact Us
    7. Acknowledgments
  4. I. Cocoa Overview and Foundation
    1. 1. Introduction to Cocoa
      1. The Mac OS X Programming Environment
      2. Cocoa Defined
        1. The History of Cocoa
        2. Cocoa’s Feature Set
      3. The Cocoa Frameworks
      4. Languages
        1. Objective-C
        2. Java
        3. AppleScript
      5. The Foundation Framework
        1. Programming Types and Operations
        2. Operating System Entities and Services
        3. Object Functionality
        4. File and I/O Management
        5. Other Services
      6. The Application Kit Framework
        1. User Interface
        2. Feature Integration
        3. Other Facilities
    2. 2. Cocoa Development Tools
      1. Installing the Developer Tools
        1. Installing from the Developer Tools CD
        2. Installing from the ADC Site
        3. Upgrading Your Tools
        4. Project Builder
        5. Say “Hello, World”
          1. Open Project Builder
          2. Creating a new project
          3. Building the project
          4. Solving build problems
          5. Running the application
        6. Using the Debugger
      2. Interface Builder
        1. Graphical “Hello, World”
          1. Open the main nib file
      3. Other Tools
        1. Command-Line Tools
      4. Exercises
    3. 3. Object-Oriented Programming with Objective-C
      1. Introducing Objects
        1. Classes of Objects
        2. Inheritance
      2. Creating and Using Objects
        1. Working with Multiple Objects
      3. Methods and Messages
        1. Arguments in Messages
        2. Nested Messages
        3. How Messaging Works
      4. Objective-C-Defined Types
      5. Creating New Classes
        1. Defining the Song Interface
        2. Defining the Song Implementation
        3. Using the Song Class
      6. Overriding Methods
        1. Calling Superclass Methods
        2. Object Creation
          1. Designated initializers
        3. Object Deallocation
      7. Other Concepts
      8. Exercises
    4. 4. The Cocoa Foundation Kit
      1. Strings
        1. Basic String Operations
          1. Setting breakpoints and debugging
        2. Working with Portions of a String
        3. Mutable Strings
        4. Working with Files
      2. Collections
        1. Arrays
          1. Using the debugger to explore NSArray
        2. Mutable Arrays
          1. Exploring NSMutableArray with the debugger
        3. Arrays and the Address Book
        4. Sets
        5. Dictionaries
        6. Mutable Dictionaries
        7. Storing Collections as Files
      3. Memory Management
        1. Object Initialization and Deallocation
        2. Reference Counting
        3. Autorelease Pools
        4. Retaining Objects in Accessor Methods
        5. Rules of Thumb
      4. Exercises
  5. II. Single-Window Applications
    1. 5. Graphical User Interfaces
      1. Graphical User Interfaces in Cocoa
        1. Windows
        2. Nib Files
        3. Outlets
        4. Actions
      2. Designing Applications Using MVC
        1. MVC in Currency Converter’s Design
      3. Create the Currency Converter Project
      4. Create the Interface
        1. Open the Main Nib File
        2. Resize the Window
        3. Set the Window’s Title and Attributes
        4. Place the Text Fields
        5. Duplicating Objects
        6. Change the Attributes of a Text Field
        7. Add Text Labels
        8. Add a Button to the Interface
        9. Adding a Decorative Line
        10. Setting the Initial First Responder and Enabling Tabbing
        11. Test the Interface
      5. Define the Classes
        1. Create the Controller Class
          1. Define outlets for the controller
          2. Define actions for the controller
        2. Define the Model Class
      6. Connect the Model, Controller, and View
        1. Generate an Instance of the Controller and Model
        2. Connect the Controller to the Interface
        3. Connect the Model to the Controller
      7. Implement the Classes
        1. Generate the Source Files
        2. Examine an Interface (Header) File in Project Builder
        3. Add the Conversion Method
      8. Build and Run
      9. Exercises
    2. 6. Windows, Views, and Controls
      1. Windows and the Window System
        1. Window, View, and Application
        2. Key and Main Windows
        3. The Window Menu
        4. Panels
      2. The View Hierarchy
      3. Coordinate Systems
        1. Screen Coordinate System
        2. Window Coordinate System
        3. View Coordinate System
      4. Controls, Cells, and Formatters
        1. Cells and Formatters
        2. A Formatted Cell Example
          1. Open the main nib file
          2. Create the user interface
          3. Add a text field
          4. Create a controller
          5. Define an outlet
          6. Generate a controller instance
          7. Connect the controller to the interface
          8. Generate the source files
          9. Statically type the outlet
          10. Implement the awakeFromNib method
          11. Add the formatter
      5. Targets and Actions
        1. Target/Action Example
          1. Add a Refresh button
          2. Define an action
          3. Connect the button to the action
          4. Update the source files
          5. Implement the action method
      6. Exercises
    3. 7. Custom Views
      1. Custom View Creation Steps
      2. Create a Custom View
        1. Open the Main Nib File
        2. Define a Subclass of NSView
        3. Add a Custom View to the Main Window
        4. Implement the Drawing Method
        5. Autosizing of Views
      3. Drawing into a View: Behind the Scenes
      4. Draw Strings into a View
        1. Create a Custom View
        2. Implement the Drawing Method
        3. Drawing Strings with Attributes
      5. Draw Paths into a View
        1. Create a Custom View
        2. Implement the Drawing Method
        3. Draw an Oval Path
      6. Exercises
    4. 8. Event Handling
      1. Events
        1. The Event Cycle
        2. Responders
        3. First Responder
        4. Event Routing
      2. Dot View Application
        1. Create the DotView Class
        2. Create the Interface
        3. Connect the Controls to DotView
        4. Define the DotView Header
        5. Define the DotView Class
          1. Implement the initWithFrame: method
          2. Implement the awakeFromNib: method
          3. Implement the dealloc method
          4. Implement the drawRect: method
          5. Implement the isOpaque method
          6. Implement the mouseDown: method
          7. Implement the setColor: action method
          8. Implement the setRadius: action method
      3. Event Delegation
        1. Create a Delegate
        2. Delegation Using Sheets
      4. Notifications
        1. How Notifications Work
        2. Notifications in Action
        3. Memory-Management Considerations
      5. Exercises
    5. 9. Models and Data Functionality
      1. Protocols
      2. Key-Value Coding
        1. A Key-Value Coding Example
      3. Table Views
      4. Table View Example
        1. Create the Interface
        2. Configure the Table Columns
        3. Declare the Data Source Class
        4. Create the Data Source
        5. Allow Modification of Table Entries
        6. Adding Entries to the Model
      5. Saving Data: Coding and Archiving
        1. Adding Coding and Archiving to the Menu Application
      6. Using Formatters
      7. Sorting Tables
      8. Exercises
  6. III. Document-Based Applications
    1. 10. Multiple Document Architecture
      1. Architectural Overview
        1. The Document Object
        2. The Document Controller
        3. The Window Controller
        4. Memory Management
      2. Building a Document-Based Application
        1. Document-Based Application Template
        2. Create the Project
        3. Examine the Document Interface
        4. Examine the Document Implementation
        5. Compose the Interface
        6. Modify the Info Property List
        7. Implement the MyDocument Class
      3. Exercises
    2. 11. Rich-Text Handling
      1. Cocoa’s Text System
        1. Supported Text Data Types
        2. Working with File Wrappers
      2. Creating a Rich-Text Editor
      3. Enabling the Font Menu
      4. Text Storage and Attributed Text
        1. Working with Attributed Text
        2. Registering Undo Actions
      5. Enabling the Text Menu
      6. Handling Embedded Images
        1. The Spoken Word
      7. Exercises
  7. IV. Miscellaneous Topics
    1. 12. Printing
      1. Printing a View
      2. Using Print Operations
        1. Printing Using the Document Architecture
      3. Setting Margins
      4. Exercises
    2. 13. Bundles and Resources
      1. Peeking Inside Bundles
        1. Bundle Directories
      2. Using Bundles
        1. A Performance Diversion
      3. Exercises
    3. 14. Localization
      1. Mac OS X Language Preferences
      2. Localizing Resources
        1. Localized Resource Search Algorithm
      3. Localizing Nib Files
      4. Localizing Strings
      5. Exercises
    4. 15. Defaults and Preferences
      1. How Preferences Work
      2. Using Defaults
      3. Command-Line Preferences Access
      4. Using Unique Application Identifiers
      5. Exercises
    5. 16. Accessory Windows
      1. The Role of File’s Owner
      2. Making an Info Window
        1. Create the Main Interface
        2. Create the Inspector Panel
        3. Implement the Code
        4. Tracking Changes with Notifications
      3. Exercises
    6. 17. Finishing Touches
      1. Tidying Up the User Interface
      2. Providing an Icon
      3. Providing Help
      4. Customizing the About Box
        1. Giving Some Credit
      5. Tweaking Compiler Settings
      6. Packaging for Distribution
      7. Closure
      8. Exercises
  8. V. Appendixes
    1. A. Exercise Solutions
      1. Chapter 2
      2. Chapter 3
      3. Chapter 4
      4. Chapter 5
      5. Chapter 6
      6. Chapter 7
      7. Chapter 8
      8. Chapter 9
      9. Chapter 10
      10. Chapter 11
      11. Chapter 12
      12. Chapter 13
      13. Chapter 14
      14. Chapter 15
      15. Chapter 16
      16. Chapter 17
    2. B. Additional Resources
      1. Documentation on Your Hard Drive
      2. Printed Documentation
      3. Getting Sample Code
      4. Web Sites
      5. Mailing Lists
      6. Partnering with Apple
    3. C. Using the Foundation and Application Kit API References
      1. Cocoa Browser
  9. Index
  10. About the Authors
  11. Colophon
  12. Copyright