You are previewing Programming in Objective-C, Sixth Edition.
O'Reilly logo
Programming in Objective-C, Sixth Edition

Book Description

Updated for OS X 10.9 Mavericks, iOS 7, and Xcode 5

Programming in Objective-C is a concise, carefully written tutorial on the basics of Objective-C and object-oriented programming for Apple's iOS and OS X platforms.

The book makes no assumptions about prior experience with object-oriented programming languages or with the C language (which Objective-C is based upon). Because of this, both beginners and experienced programmers alike can use this book to quickly and effectively learn the fundamentals of Objective-C. Readers can also learn the concepts of object-oriented programming without having to first learn all of the intricacies of the underlying C programming language.

This unique approach to learning, combined with many small program examples and exercises at the end of each chapter, makes Programming in Objective-C ideally suited for either classroom use or self-study.

This edition has been fully updated to incorporate new Objective-C features and technologies introduced with Xcode 5, iOS 7, and Mac OS X Mavericks.

“The best book on any programming language that I’ve ever read. If you want to learn Objective-C, buy it.”—Calvin Wolcott

“An excellent resource for a new programmer who wants to learn Objective-C as their first programming language—a woefully underserved market.”—Pat Hughes

Table of Contents

  1. About This eBook
  2. Title Page
  3. Copyright Page
  4. Developer’s Library
  5. Dedication Page
  6. Contents at a Glance
  7. Table of Contents
  8. About the Author
  9. About the Technical Reviewers
  10. We Want to Hear from You!
  11. Reader Services
  12. 1. Introduction
    1. What You Will Learn from This Book
    2. How This Book Is Organized
    3. Support
    4. Acknowledgments
    5. Preface to the Sixth Edition
  13. I: The Objective-C Language
    1. 2. Programming in Objective-C
      1. Compiling and Running Programs
        1. Using Xcode
        2. Using Terminal
      2. Explanation of Your First Program
      3. Displaying the Values of Variables
      4. Summary
      5. Exercises
    2. 3. Classes, Objects, and Methods
      1. What Is an Object, Anyway?
      2. Instances and Methods
      3. An Objective-C Class for Working with Fractions
      4. The @interface Section
        1. Choosing Names
        2. Class and Instance Methods
      5. The @implementation Section
      6. The program Section
      7. Accessing Instance Variables and Data Encapsulation
      8. Summary
      9. Exercises
    3. 4. Data Types and Expressions
      1. Data Types and Constants
        1. Type int
        2. Type float
        3. Type char
        4. Qualifiers: long, long long, short, unsigned, and signed
        5. Type id
      2. Arithmetic Expressions
        1. Operator Precedence
        2. Integer Arithmetic and the Unary Minus Operator
        3. The Modulus Operator
        4. Integer and Floating-Point Conversions
        5. The Type Cast Operator
      3. Assignment Operators
      4. A Calculator Class
      5. Exercises
    4. 5. Program Looping
      1. The for Statement
        1. Keyboard Input
        2. Nested for Loops
        3. for Loop Variants
      2. The while Statement
      3. The do Statement
      4. The break Statement
      5. The continue Statement
      6. Summary
      7. Exercises
    5. 6. Making Decisions
      1. The if Statement
        1. The if-else Construct
        2. Compound Relational Tests
        3. Nested if Statements
        4. The else if Construct
      2. The switch Statement
      3. Boolean Variables
      4. The Conditional Operator
      5. Exercises
    6. 7. More on Classes
      1. Separate Interface and Implementation Files
      2. Synthesized Accessor Methods
      3. Accessing Properties Using the Dot Operator
      4. Multiple Arguments to Methods
        1. Methods without Argument Names
        2. Operations on Fractions
      5. Local Variables
        1. Method Arguments
        2. The static Keyword
      6. The self Keyword
      7. Allocating and Returning Objects from Methods
        1. Extending Class Definitions and the Interface File
      8. Exercises
    7. 8. Inheritance
      1. It All Begins at the Root
        1. Finding the Right Method
      2. Extension through Inheritance: Adding New Methods
        1. A Point Class and Object Allocation
        2. The @class Directive
        3. Classes Owning Their Objects
      3. Overriding Methods
        1. Which Method Is Selected?
      4. Abstract Classes
      5. Exercises
    8. 9. Polymorphism, Dynamic Typing, and Dynamic Binding
      1. Polymorphism: Same Name, Different Class
      2. Dynamic Binding and the id Type
      3. Compile Time Versus Runtime Checking
      4. The id Data Type and Static Typing
        1. Argument and Return Types with Dynamic Typing
      5. Asking Questions about Classes
      6. Exception Handling Using @try
      7. Exercises
    9. 10. More on Variables and Data Types
      1. Initializing Objects
      2. Scope Revisited
        1. More on Properties, Synthesized Accessors, and Instance Variables
        2. Global Variables
        3. Static Variables
      3. Enumerated Data Types
      4. The typedef Statement
      5. Data Type Conversions
        1. Conversion Rules
      6. Bit Operators
        1. The Bitwise AND Operator
        2. The Bitwise Inclusive-OR Operator
        3. The Bitwise Exclusive-OR Operator
        4. The Ones Complement Operator
        5. The Left-Shift Operator
        6. The Right-Shift Operator
      7. Exercises
    10. 11. Categories and Protocols
      1. Categories
      2. Class Extensions
        1. Some Notes about Categories
      3. Protocols and Delegation
        1. Delegation
        2. Informal Protocols
      4. Composite Objects
      5. Exercises
    11. 12. The Preprocessor
      1. The #define Statement
        1. More Advanced Types of Definitions
      2. The #import Statement
      3. Conditional Compilation
        1. The #ifdef, #endif, #else, and #ifndef Statements
        2. The #if and #elif Preprocessor Statements
        3. The #undef Statement
      4. Exercises
    12. 13. Underlying C Language Features
      1. Arrays
        1. Initializing Array Elements
        2. Character Arrays
        3. Multidimensional Arrays
      2. Functions
        1. Arguments and Local Variables
        2. Returning Function Results
        3. Functions, Methods, and Arrays
      3. Blocks
      4. Structures
        1. Initializing Structures
        2. Structures within Structures
        3. Additional Details about Structures
        4. Don’t Forget about Object-Oriented Programming!
      5. Pointers
        1. Pointers and Structures
        2. Pointers, Methods, and Functions
        3. Pointers and Arrays
        4. Operations on Pointers
        5. Pointers and Memory Addresses
      6. They’re Not Objects!
      7. Miscellaneous Language Features
        1. Compound Literals
        2. The goto Statement
        3. The Null Statement
        4. The Comma Operator
        5. The sizeof Operator
        6. Command-Line Arguments
      8. How Things Work
        1. Fact 1: Instance Variables Are Stored in Structures
        2. Fact 2: An Object Variable Is Really a Pointer
        3. Fact 3: Methods Are Functions, and Message Expressions Are Function Calls
        4. Fact 4: The id Type Is a Generic Pointer Type
      9. Exercises
  14. II: The Foundation Framework
    1. 14. Introduction to the Foundation Framework
      1. Foundation Documentation
    2. 15. Numbers, Strings, and Collections
      1. Number Objects
      2. String Objects
        1. More on the NSLog Function
        2. The description Method
        3. Mutable Versus Immutable Objects
        4. Mutable Strings
      3. Array Objects
        1. Making an Address Book
        2. Sorting Arrays
      4. Dictionary Objects
        1. Enumerating a Dictionary
      5. Set Objects
        1. NSIndexSet
      6. Exercises
    3. 16. Working with Files
      1. Managing Files and Directories: NSFileManager
        1. Working with the NSData Class
        2. Working with Directories
        3. Enumerating the Contents of a Directory
      2. Working with Paths: NSPathUtilities.h
        1. Common Methods for Working with Paths
        2. Copying Files and Using the NSProcessInfo Class
      3. Basic File Operations: NSFileHandle
      4. The NSURL Class
      5. The NSBundle Class
      6. Exercises
    4. 17. Memory Management and Automatic Reference Counting
      1. Automatic Garbage Collection
      2. Manual Reference Counting
        1. Object References and the Autorelease Pool
      3. The Event Loop and Memory Allocation
      4. Summary of Manual Memory Management Rules
      5. Automatic Reference Counting
      6. Strong Variables
      7. Weak Variables
      8. @autoreleasepool Blocks
      9. Method Names and Non-ARC Compiled Code
    5. 18. Copying Objects
      1. The copy and mutableCopy Methods
      2. Shallow Versus Deep Copying
      3. Implementing the <NSCopying> Protocol
      4. Copying Objects in Setter and Getter Methods
      5. Exercises
    6. 19. Archiving
      1. Archiving with XML Property Lists
      2. Archiving with NSKeyedArchiver
      3. Writing Encoding and Decoding Methods
      4. Using NSData to Create Custom Archives
      5. Using the Archiver to Copy Objects
      6. Exercises
  15. III: Cocoa, Cocoa Touch, and the iOS SDK
    1. 20. Introduction to Cocoa and Cocoa Touch
      1. Framework Layers
      2. Cocoa Touch
    2. 21. Writing iOS Applications
      1. The iOS SDK
      2. Your First iPhone Application
        1. Creating a New iPhone Application Project
        2. Entering Your Code
        3. Designing the Interface
      3. An iPhone Fraction Calculator
        1. Starting the New Fraction Calculator Project
        2. Defining the View Controller
        3. The Fraction Class
        4. A Calculator Class That Deals with Fractions
        5. Designing the User Interface
      4. Summary
      5. Exercises
  16. Appendixes
    1. Appendix A. Glossary
    2. Appendix B. Address Book Example Source Code
  17. Index