O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Cocoa and Objective-C: Up and Running

Book Description

Build solid applications for Mac OS X, iPhone, and iPod Touch, regardless of whether you have basic programming skills or years of programming experience. With this book, you'll learn how to use Apple's Cocoa framework and the Objective-C language through step-by-step tutorials, hands-on exercises, clear examples, and sound advice from a Cocoa expert.

Cocoa and Objective-C: Up and Running offers just enough theory to ground you, then shows you how to use Apple's rapid development tools -- Xcode and Interface Builder -- to develop Cocoa applications, manage user interaction, create great UIs, and more. You'll quickly gain the experience you need to develop sophisticated Apple software, whether you're somewhat new to programming or just new to this platform.

  • Get a quick hands-on tour of basic programming skills with the C language
  • Learn how to use Interface Builder to quickly design and prototype your application's user interface
  • Start using Objective-C by creating objects and learning memory management
  • Learn about the Model-View-Controller (MVC) method of sharing data between objects
  • Understand the Foundation value classes, Cocoa's robust API for storing common data types
  • Become familiar with Apple's graphics frameworks, and learn how to make custom views with AppKit

Table of Contents

  1. A Note Regarding Supplemental Files
  2. Preface
    1. Who This Book Is For
    2. How This Book Is Organized
    3. Conventions Used in This Book
    4. Using Code Examples
    5. Safari® Books Online
    6. How to Contact Us
    7. Acknowledgments
  3. 1. Setup and First Run
    1. Download and Install Xcode
    2. Your First Application
      1. Create the Interface
        1. Set sizing properties
      2. Run the Finished Application
  4. 2. Thinking in Code: Basic C
    1. How Code Works
    2. How to Format Code
    3. Variables
      1. Types
      2. Constants
      3. Enumerated Types
      4. Typedefs
    4. Functions
      1. Declaring Functions
    5. Example: FirstProgram
      1. Displaying Values on the Command Line
      2. Compile and Run the Example
    6. Scope
      1. Static Variables
    7. Conditionals
    8. Example: ShoppingTrip
      1. Compile and Run
    9. Wrap Up
  5. 3. Memory and Pointers: Advanced C
    1. Arrays
    2. Loops
    3. Text Strings
    4. Multidimensional Arrays
    5. Pointers
      1. The Purpose of Pointers
      2. Using Pointers
      3. Pointers and the const Keyword
    6. Dynamic Memory
    7. Strings and Dynamic Memory
      1. Returning Strings from Functions
    8. Arrays of Strings
    9. Example: AddressBook
      1. Compile and Run the AddressBook Example
    10. Structs
    11. Header Files
    12. Compile and Run the HeaderFileTest Example
    13. Create Files for the Song Struct
    14. Final Example
  6. 4. Thinking in Objects
    1. Structs and Classes
      1. Designing Classes
    2. Accessors
    3. Inheritance
    4. Composition
    5. Object Lifetime
    6. Built-in Classes
  7. 5. Basic Objective-C
    1. NSString Basics
    2. Using Methods
      1. Nested Method Calls
      2. Multi-Input Methods
    3. Accessors
      1. Dot Syntax
    4. Creating Objects
    5. Basic Memory Management
      1. Using Autorelease Directly
    6. Declaring a Class
      1. Add Methods
    7. Implementing a Class
      1. init
      2. dealloc
    8. Example: PhotoInfo
  8. 6. More Objective-C
    1. More on Memory Management
      1. The Life of an Instance Variable
      2. Copying Objects
    2. Class Name Prefixes
    3. Properties
      1. Property Options
        1. Options for accessor implementations
    4. 64-Bit Objective-C
      1. Enabling 64-Bit
      2. Should I Use 64-Bit?
      3. All Further Examples Assume 64-Bit
    5. Categories
      1. Categories for Private Methods
    6. Introspection
    7. Protocols
    8. Dynamic Messaging
      1. Using Selectors to Call Methods
      2. Forwarding Messages
    9. Exceptions
    10. Example: DataCollector
      1. Some New Classes and Methods
      2. Create the Files
      3. Build and Run
  9. 7. Foundation Value Classes
    1. NSString
      1. Ranges and Substrings
      2. Using NSString with C Types
      3. Comparing Strings for Equality
      4. Strings As File Paths
      5. Reading and Writing Files with Strings
    2. Mutability
      1. Advantages of Mutability
      2. Advantages of Immutability
    3. Core Foundation
      1. Memory Management
      2. Core Foundation Mutability
      3. Toll-Free Bridging
      4. Core Foundation Types As Properties
      5. Drawbacks of Core Foundation Types
      6. Open Source
    4. NSNumber
      1. CFNumberRef
      2. Cocoa Primitive Types
      3. NSDecimalNumber
      4. NSNumberFormatter
      5. When to Use Which Number Type
    5. NSData
      1. NSMutableData
        1. CFDataRef and CFMutableDataRef
    6. NSArray
      1. Fast Enumeration
      2. Blocks
      3. NSMutableArray
      4. CFArrayRef
      5. NSIndexSet
    7. NSDictionary
      1. NSMutableDictionary
      2. CFDictionaryRef
    8. NSSet
      1. NSMutableSet
    9. NSValue
    10. NSDate
      1. CFDateRef
  10. 8. Basic Controls
    1. How to Use This Chapter
    2. Windows and Views
    3. Targets and Actions
      1. Buttons
      2. Declaring Action Methods
      3. Connecting Actions
        1. Connecting actions from the HUD
        2. Connecting actions in code
      4. Menus
      5. Responder Chain
        1. Two meanings of first responder
      6. Pop-up Buttons
      7. Sliders
      8. Text Fields
    4. Outlets
    5. Datasources
      1. Table View Datasource Methods
      2. Implementing Datasource Methods
    6. Bindings
      1. Key-Value Protocols
        1. Key-Value Coding
      2. Bindings for Simple Controls
        1. Connect the bindings
        2. Run the application
      3. Bindings for Complex Controls
      4. Tips for Debugging Bindings
  11. 9. Designing Applications Using MVC
    1. About This Project
      1. Window Controllers
      2. View Controllers
      3. Core Data
    2. Create the Project Files
      1. Create the Entities
      2. Add Attributes and Relationships
      3. Update the App Delegate
      4. Add the Quartz Framework
      5. Create the Window Controller
      6. Create the View Controllers
        1. Browser view controller
        2. Editor view controller
        3. List view controller
      7. Create the Managed Object Classes
        1. Implement the photo class
        2. Implement the album class
    3. Create the User Interface
      1. Remove the Default Window
        1. Add the album menu
      2. Create the Main Window Interface
        1. Set window sizing
        2. Add controls
        3. Set up bindings
        4. Connect outlets and actions
      3. Create the Browser Interface
        1. Set up the album table
        2. Set up the image browser
        3. Set up bindings
        4. Connect outlets
      4. Create the Editor View Interface
      5. Create the List View Interface
        1. Set up bindings
        2. Connect outlets
    4. Run the Application
    5. Preparing for Release
  12. 10. Custom Views and Drawing
    1. Basic Geometry
      1. Geometry Structs As Strings
      2. Geometry Structs As NSValues
      3. Cocoa View Coordinates
      4. Derived Rects
      5. Comparison Functions
    2. Basic Drawing
      1. NSColor
      2. Subclassing NSView
        1. When to draw
      3. Instantiate the View
        1. Setting resizing values in code
      4. The Graphics Context
    3. Bezier Paths
      1. Drawing Polygons
      2. Drawing Curved Paths
    4. Images
      1. Loading Image Data
        1. Loading an image from your project
        2. Loading standard Cocoa artwork
      2. Drawing Images in a View
        1. Preserving aspect ratio
    5. Shadows
      1. Add a Shadow to StyledImageView
    6. Gradients
      1. Drawing a Gradient Background
      2. Drawing an Image Sheen
        1. Calculate the sheen path
        2. Add sheen properties and methods
    7. Refactoring View Code
      1. Why You Should Refactor
      2. Goals for Refactoring
        1. Symmetry in practice
      3. Refactored Header
      4. Refactored Implementation
      5. Test the Refactored Version
    8. Text
      1. Fonts
      2. Attributed Strings
        1. Paragraph styles
        2. Text sizes and vertical alignment
      3. Add a Title to StyledImageView
    9. Handling Mouse and Keyboard Events
      1. Keyboard Events
      2. Mouse Events
      3. Add Event Support to StyledImageView
  13. 11. The Final Word
    1. The List
    2. Websites
    3. Last Thought
  14. Index
  15. About the Author
  16. Colophon
  17. Copyright