You are previewing Cocoa and Objective-C: Up and Running.
O'Reilly logo
Cocoa and Objective-C: Up and Running

Book Description

Learn the tools most essential for building high-quality applications for Mac OS X, iPhone, and iPod touch. This thorough introduction to Apple's Cocoa programming environment and its Objective-C language quickly gets you up to speed with step-by-step tutorials, hands-on tasks, and numerous examples. With clear instruction from Scott Stevenson, the Cocoa programmer behind Cocoa Dev Central, you'll learn just enough foundation and theory to ground you before jumping right into building applications, managing events, creating GUIs, and more with Cocoa and the Application Kit. You also get skill-building exercises, and key input from a Cocoa expert on the best use of these tools. This book is still in progress, but you can get going on this technology through our Rough Cuts edition, which lets you read the manuscript as it's being written , either online or via PDF.

Table of Contents

  1. Special Upgrade Offer
  2. A Note Regarding Supplemental Files
  3. 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
  4. 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
  5. 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
  6. 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
  7. 4. Thinking in Objects
    1. Structs and Classes
      1. Designing Classes
    2. Accessors
    3. Inheritance
    4. Composition
    5. Object Lifetime
    6. Built-in Classes
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 11. The Final Word
    1. The List
    2. Websites
    3. Last Thought
  15. Index
  16. About the Author
  17. Colophon
  18. Special Upgrade Offer
  19. Copyright