You are previewing Objective-C.
O'Reilly logo
Objective-C

Book Description

A soup-to-nuts guide on the Objective-C programming language

Objective-C is the language behind Cocoa and Cocoa Touch, which is the Framework of applications written for the Macintosh, iPod touch, iPhone, and iPad platforms. Part of the Developer Reference series covering the hottest Apple topics, this book covers everything from the basics of the C language to advanced aspects of Apple development. You'll examine Objective-C and high-level subjects of frameworks, threading, networking, and much more.

  • Covers the basics of the C language and then quickly moves onto Objective-C and more advanced topics

  • Draws from the author's first-hand experience garnered while developing applications for the Mac and iPhone OS platforms

  • Includes chapters on classes, memory management, threads, and the Foundation framework

  • Also covers advanced topics like protocols, categories, associated objects, and blocks

Featuring real-life examples drawn from the author's experience, Objective-C offers an insider look at this amazing programming language.

Table of Contents

  1. Copyright
  2. About the Author
  3. Credits
  4. Preface
  5. Acknowledgments
  6. I. Introducing Objective-C
    1. 1. Introducing Objective-C
      1. 1.1. Using Xcode for Development
        1. 1.1.1. Starting a project
        2. 1.1.2. Exploring the files in your project
        3. 1.1.3. Adding source files
        4. 1.1.4. The main Xcode window
      2. 1.2. Understanding the Compilation Process
        1. 1.2.1. Writing code
        2. 1.2.2. Turning source code into compiled code and compiled code into an executable
        3. 1.2.3. Exploring application bundles
        4. 1.2.4. Working with build settings
      3. 1.3. Using the Xcode Static Analyzer
      4. 1.4. Understanding the Objective-C Runtime
      5. 1.5. Summary
    2. 2. Understanding Basic Syntax
      1. 2.1. Working with Statements and Expressions
        1. 2.1.1. Declaring variables
        2. 2.1.2. Using comments
        3. 2.1.3. Exploring scalar types
        4. 2.1.4. Using special variable modifiers
        5. 2.1.5. Understanding structures
        6. 2.1.6. Using typedefs
        7. 2.1.7. Using enum
        8. 2.1.8. Understanding pointers
        9. 2.1.9. Using operators
        10. 2.1.10. The ternary operator
      2. 2.2. Working with Functions
        1. 2.2.1. Understanding functions
        2. 2.2.2. Defining functions
        3. 2.2.3. Understanding implementation versus interface
        4. 2.2.4. Linking with implementation files
      3. 2.3. Controlling Program Flow
        1. 2.3.1. Using conditional statements
          1. 2.3.1.1. Using if-else
          2. 2.3.1.2. Using the ternary conditional
          3. 2.3.1.3. Using switch statements
          4. 2.3.1.4. Choosing among conditional statements
        2. 2.3.2. Working with loops
          1. 2.3.2.1. Working with for
          2. 2.3.2.2. Traditional for loops
          3. 2.3.2.3. Using for for fast enumeration
          4. 2.3.2.4. Working with while
          5. 2.3.2.5. Using do
      4. 2.4. Applying What You Have Learned
      5. 2.5. Summary
    3. 3. Adding Objects
      1. 3.1. Understanding Objects
        1. 3.1.1.
          1. 3.1.1.1. Understanding inheritance
          2. 3.1.1.2. Using polymorphism
          3. 3.1.1.3. Using the id datatype
        2. 3.1.2. Creating classes
          1. 3.1.2.1. Working with class files
          2. 3.1.2.2. Writing object methods
          3. 3.1.2.3. Working with the special object methods
          4. 3.1.2.4. Writing class methods
        3. 3.1.3. Declaring objects
        4. 3.1.4. Making calls on objects
      2. 3.2. Working with Properties
        1. 3.2.1. Understanding the differences between state and behavior
          1. 3.2.1.1. Using properties to declare object state
          2. 3.2.1.2. Understanding synthesized property accessors
          3. 3.2.1.3. Using the nonatomic attribute
          4. 3.2.1.4. Using the assign, retain, and copy Attributes
          5. 3.2.1.5. Using properties with different data member names
        2. 3.2.2. Using dot notation
      3. 3.3. Applying Objects
        1. 3.3.1. Creating the employee class
        2. 3.3.2. Creating the manager class
        3. 3.3.3. Tying the classes together in the HR main
    4. 4. Understanding Objective-C Memory Management
      1. 4.1. Using Reference Counting
        1. 4.1.1. Learning the memory management rules
        2. 4.1.2. Using autorelease
          1. 4.1.2.1. Understanding autorelease pools
        3. 4.1.3. Understanding memory from inside the object
          1. 4.1.3.1. Writing initializers
          2. 4.1.3.2. Writing dealloc Methods
      2. 4.2. Using Garbage Collection
        1. 4.2.1. Understanding the Garbage Collector
          1. 4.2.1.1. Understanding reference types
        2. 4.2.2. Configuring your project for garbage collection
        3. 4.2.3. Using Frameworks in a Garbage Collected Project
      3. 4.3. Exploring Key Garbage Collector Patterns
        1. 4.3.1. Managing finite resources
        2. 4.3.2. Writing foundation applications with garbage collection
        3. 4.3.3. Working with objects in nib files
        4. 4.3.4. Forcing garbage collection
        5. 4.3.5. Working with void pointers and garbage collection
        6. 4.3.6. Using the object oriented interface to the garbage collector
      4. 4.4. Understanding What Memory Management Model to Use for Your Projects
      5. 4.5. Summary
  7. II. Exploring Deeper Features
    1. 5. Working with Blocks
      1. 5.1. Understanding Blocks
        1. 5.1.1. Declaring code blocks
        2. 5.1.2. Using code blocks
      2. 5.2. Understanding Important Block Scoping
        1. 5.2.1. Managing code block memory
        2. 5.2.2. Making blocks easier to read with typedef
      3. 5.3. Using Blocks with Threads
        1. 5.3.1. Working with Grand Central Dispatch
        2. 5.3.2. Using GCD functions to dispatch code blocks in threads
      4. 5.4. Working with Common Block Design Patterns
        1. 5.4.1. Using code blocks for maps
        2. 5.4.2. Using Blocks in the Standard API
      5. 5.5. Applying Blocks to an Embarrassingly Parallel Task
        1. 5.5.1. Creating the project
        2. 5.5.2. Using blocks with an array to filter for primes
        3. 5.5.3. Using Grand Central Dispatch
      6. 5.6. Summary
    2. 6. Using Key Value Coding and Key Value Observing
      1. 6.1. Accessing Object Properties Using Key Value Coding
        1. 6.1.1. Working with key paths
        2. 6.1.2. Writing KVC compliant accessors
        3. 6.1.3. Using KVC with arrays
          1. 6.1.3.1. Using indexed accessors
          2. 6.1.3.2. Using Unordered Accessors
        4. 6.1.4. Using KVC with structures and scalars
        5. 6.1.5. Searching objects for attributes
      2. 6.2. Observing Changes to KVC-Compliant Values
        1. 6.2.1. Using KVO
        2. 6.2.2. Registering as an observer
        3. 6.2.3. Defining callbacks for KVO
        4. 6.2.4. Removing an observer
        5. 6.2.5. Implementing manual notifications
        6. 6.2.6. Understanding risks with KVO
      3. 6.3. Applying Key Value Observing
      4. 6.4. Summary
    3. 7. Working with Protocols
      1. 7.1. Favoring Composition Over Inheritance
        1. 7.1.1. Understanding why you don't need (or want) multiple inheritance
        2. 7.1.2. Understanding how protocols solve the problem
        3. 7.1.3. Documenting an expected interface for others to implement
      2. 7.2. Implementing Protocols in Your Objects
        1. 7.2.1. Declaring a protocol
        2. 7.2.2. Declaring that a class implements a protocol
        3. 7.2.3. Declaring an object that must implement a protocol
        4. 7.2.4. Exploring formal and informal protocols
        5. 7.2.5. Determining if an object implements optional methods
        6. 7.2.6. Avoiding Circular Protocol Dependencies
      3. 7.3. Exploring examples of protocol use
      4. 7.4. Summary
    4. 8. Extending Existing Class Capabilities
      1. 8.1. Working with Third-Party Frameworks and Classes
      2. 8.2. Working with Categories
        1. 8.2.1. Declaring categories
        2. 8.2.2. Implementing category methods
        3. 8.2.3. Declaring categories in header files
        4. 8.2.4. Using a category
        5. 8.2.5. Breaking up functionality using categories
        6. 8.2.6. Extending class methods
        7. 8.2.7. Exploring category limitations
        8. 8.2.8. Implementing protocols with categories
        9. 8.2.9. Understanding the dangers of creating categories on NSObject
      3. 8.3. Extending Classes using Anonymous Categories
      4. 8.4. Associating Variables with Existing Classes
      5. 8.5. Summary
    5. 9. Writing Macros
      1. 9.1. Reviewing the Compilation Process
        1. 9.1.1. Understanding how macros work
      2. 9.2. Defining Macros
        1. 9.2.1. Defining constants
        2. 9.2.2. Passing constants by compilation
        3. 9.2.3. Using variables in macros
        4. 9.2.4. Using stringification
        5. 9.2.5. Handling conditional switching
        6. 9.2.6. Using built-in macros
      3. 9.3. Summary
    6. 10. Handling Errors
      1. 10.1. Classifying Errors
        1. 10.1.1. Understanding how to interrupt program flow
      2. 10.2. Using the Different Mechanisms for Error Handling
        1. 10.2.1. Using return codes
        2. 10.2.2. Using exceptions
        3. 10.2.3. Using NSError
          1. 10.2.3.1. Creating an NSError Object
          2. 10.2.3.2. Understanding the NSError userInfo Dictionary
          3. 10.2.3.3. Working with a recovery attempter
          4. 10.2.3.4. Working with NSErrors in methods
      3. 10.3. Summary
  8. III. Using the Foundation Framework
    1. 11. Understanding How the Frameworks Fit Together
      1. 11.1. Understanding the Foundation Framework
        1. 11.1.1. Exploring other frameworks
      2. 11.2. Using Frameworks in Your Projects
        1. 11.2.1. Adding frameworks
        2. 11.2.2. Including the headers
        3. 11.2.3. Considering garbage collection
      3. 11.3. Summary
    2. 12. Using Strings
      1. 12.1. Understanding the String Declaration Syntax
        1. 12.1.1. Using format strings
        2. 12.1.2. Working with Other NSString Methods
        3. 12.1.3. Using NSString categories
      2. 12.2. Summary
    3. 13. Working with Collections
      1. 13.1. Working with Arrays
        1. 13.1.1. Using dictionaries
        2. 13.1.2. Working with sets
        3. 13.1.3. Understanding mutability
      2. 13.2. Understanding Collections and Memory Management
        1. 13.2.1. Using specialized collections
      3. 13.3. Enumerating
      4. 13.4. Sending Messages to Elements
      5. 13.5. Sorting and Filtering
      6. 13.6. Using Blocks with Collections
      7. 13.7. Summary
    4. 14. Using NSValue, NSNumber, and NSData
      1. 14.1. Using NSValue and NSNumber
        1. 14.1.1. Wrapping arbitrary datatypes with NSValue
        2. 14.1.2. Wrapping numbers with NSNumber
        3. 14.1.3. Doing arithmetic with NSDecimalNumber
      2. 14.2. Using NSData and NSMutableData
        1. 14.2.1. Creating NSData objects
        2. 14.2.2. Accessing the raw bytes in an NSData object
      3. 14.3. Summary
    5. 15. Working with Times and Dates
      1. 15.1. Constructing Dates
        1. 15.1.1. Constructing Dates
        2. 15.1.2. Working with Time Intervals
        3. 15.1.3. Comparing dates
        4. 15.1.4. Using NSCalendar
        5. 15.1.5. Working with time zones
      2. 15.2. Using NSDateFormatter
      3. 15.3. Summary
  9. IV. Exploring Advanced Topics
    1. 16. Multiprocessing with Threads
      1. 16.1. Synchronizing Code
        1. 16.1.1. Using Locks
        2. 16.1.2. Using the @synchronize keyword
        3. 16.1.3. Understanding atomicity
      2. 16.2. Using NSThread
        1. 16.2.1. Creating threads
        2. 16.2.2. Manipulating Running Threads
        3. 16.2.3. Accessing the main thread
        4. 16.2.4. Crossing threads using perform selector
      3. 16.3. Using NSOperation and NSOperationQueue
        1. 16.3.1. Creating operations
        2. 16.3.2. Adding operations to queues
        3. 16.3.3. Manipulating queue parameters
        4. 16.3.4. Using other kinds of operations
      4. 16.4. Summary
    2. 17. Objective-C Design Patterns
      1. 17.1. Recognizing Patterns in Solutions
      2. 17.2. Describing Design Patterns in Objective-C
        1. 17.2.1. Using Singletons
          1. 17.2.1.1. Problem
          2. 17.2.1.2. Solution
          3. 17.2.1.3. Discussion
        2. 17.2.2. Delegating responsibility
          1. 17.2.2.1. Problem
          2. 17.2.2.2. Solution
          3. 17.2.2.3. Discussion
        3. 17.2.3. Notifying objects of changes
          1. 17.2.3.1. Problem
          2. 17.2.3.2. Solution
          3. 17.2.3.3. Discussion
      3. 17.3. Summary
    3. 18. Reading and Writing Data with NSCoder
      1. 18.1. Implementing the NSCoding Protocol in Your Objects
        1. 18.1.1. Implementing the NSCoding Protocol in Your Objects
        2. 18.1.2. Encoding objects
        3. 18.1.3. Encoding basic types
        4. 18.1.4. Working with object graphs
        5. 18.1.5. Using other types of data
        6. 18.1.6. Decoding objects
      2. 18.2. Using NSArchiver and NSUnarchiver
      3. 18.3. Working with Archiver File Formats and Legacy Data
      4. 18.4. Summary
    4. 19. Using Objective-C on Other Platforms
      1. 19.1. Using GNUstep
        1. 19.1.1. Using Cocotron
        2. 19.1.2. Using other open source libraries
      2. 19.2. Looking Toward the Future
      3. 19.3. Summary