You are previewing Objective-C® For Dummies®.
O'Reilly logo
Objective-C® For Dummies®

Book Description

Learn the primary programming language for creating iPhone and Mac apps

The only thing hotter than the iPhone right now is new apps for the iPhone. Objective-C is the primary language for programming iPhone and Mac OS X applications, and this book makes it easy to learn Objective-C.

Even if you have no programming experience, Objective-C For Dummies will teach you what you need to know to start creating iPhone apps. It provides an understanding of object-oriented programming in an entertaining way that helps you learn.

  • iPhone and Mac apps are hot, and most are created with Objective-C

  • Covers Xcode 3.2, which is included in Mac OS X Snow Leopard

  • Explains object-oriented programming concepts in a straightforward but fun style that makes learning easy

  • Ideal for those with no programming experience as well as those who may know other languages but are new to Objective-C

  • Prepares you to start creating iPhone and Mac OS X apps

  • Understand Mac programming concepts and patterns, and why to use them

  • Bonus CD includes all code samples used in the book

Objective-C For Dummies gives you the tools to turn your idea for an iPhone app into reality.

Note: CD-ROM/DVD and other supplementary materials are not included as part of eBook file.

Table of Contents

  1. Copyright
  2. About the Author
  3. Author's Acknowledgments
  4. Publisher's Acknowledgments
  5. Introduction
    1. About This Book
    2. Conventions Used in This Book
    3. Foolish Assumptions
    4. How This Book Is Organized
      1. Part I: Getting to the Starting Line
      2. Part II: Speaking the Language of Objective-C
      3. Part III: Walking the Object-Oriented Walk
      4. Part IV: Moving from Language to Application
      5. Part V: The Part of Tens
    5. Icons Used in This Book
  6. I. Getting to the Starting Line
    1. 1. Computer Programming Exposed!
      1. 1.1. Why a Computer Program Is Like a Peanut Butter and Jelly Sandwich
        1. 1.1.1. Examining a simple computer program
      2. 1.2. Understanding How Computer Languages Work
        1. 1.2.1. Creating a computer program
        2. 1.2.2. Running a computer program
      3. 1.3. What Is Objective-C, Anyway?
        1. 1.3.1. Understanding programming languages
        2. 1.3.2. Running your program in a runtime environment
        3. 1.3.3. Using frameworks and libraries
        4. 1.3.4. Your suite of development tools
        5. 1.3.5. Using Xcode 3.2
        6. 1.3.6. Using Objective-C Version 2.0
    2. 2. Creating Your First Program
      1. 2.1. Getting Started with the Software Development Kit
        1. 2.1.1. Registering as a Developer
        2. 2.1.2. Downloading the SDK
      2. 2.2. Creating Your Xcode Project
        1. 2.2.1. Exploring your project
        2. 2.2.2. Groups & Files view
      3. 2.3. Building and Running Your Application
        1. 2.3.1. All that stuff in First Program.m
      4. 2.4. Customizing Xcode to Your Liking
      5. 2.5. Getting to Know the Xcode Text Editor
      6. 2.6. Accessing Documentation
        1. 2.6.1. Quick Help
        2. 2.6.2. The header file for a symbol
        3. 2.6.3. Documentation window
        4. 2.6.4. Help menu
        5. 2.6.5. Find
      7. 2.7. On the CD
      8. 2.8. Working with the Examples
    3. 3. The Object in Objective-C
      1. 3.1. Not All Modules Are Created Equal
      2. 3.2. Understanding How Objects Behave
      3. 3.3. Seeing the Concepts in Action
        1. 3.3.1. Encapsulation
        2. 3.3.2. Polymorphism
      4. 3.4. Reusable Code
  7. II. Speaking the Language of Objective-C
    1. 4. Language and Programming Basics
      1. 4.1. It All Comes Down to Your Statements
        1. 4.1.1. Syntax
        2. 4.1.2. Semantics
      2. 4.2. Understanding How Variables Work
        1. 4.2.1. Data types
        2. 4.2.2. Identifier
        3. 4.2.3. Initialization
      3. 4.3. Giving Instructions with Operators
        1. 4.3.1. Really learning how operators (and everything else) work in Objective-C
        2. 4.3.2. Using arithmetic operators
        3. 4.3.3. Back to variables — floats
        4. 4.3.4. Bitwise operators
        5. 4.3.5. Compound assignment operators
        6. 4.3.6. Increment and decrement operators
        7. 4.3.7. Comma operator
        8. 4.3.8. Cast operator
        9. 4.3.9. Sizeof operator
      4. 4.4. Making Logical Decisions in Your Code
        1. 4.4.1. Boolean types
        2. 4.4.2. Relational and equality operators
        3. 4.4.3. Logical operators
        4. 4.4.4. Conditional operator
      5. 4.5. Accessing Data with Pointers
      6. 4.6. Using Constants
        1. 4.6.1. Declared constants (const)
        2. 4.6.2. Defined constants (#define)
      7. 4.7. Knowing the Objective-C Reserved Words
      8. 4.8. Congratulations
    2. 5. Functions and Data Structures
      1. 5.1. Thinking about an Application
      2. 5.2. Defining and Declaring Data Structures
      3. 5.3. Using Defined Data Types
      4. 5.4. Writing Functions
        1. 5.4.1. Scope of variables
        2. 5.4.2. Unions
        3. 5.4.3. Enumerations (enum)
      5. 5.5. Declaring Function Prototypes
      6. 5.6. Extending the Functionality of a Program
      7. 5.7. Thinking about Extensibility and Enhanceability
    3. 6. Adding a Little More Class to Your Program
      1. 6.1. Grasping Objects and Their Classes
      2. 6.2. Moving from Functions and Global Data to Objects and Classes
      3. 6.3. Creating the Interface
        1. 6.3.1. Declaring the class interface
          1. 6.3.1.1. Enter the @interface compiler directive and first line
          2. 6.3.1.2. Enter the instance variables
          3. 6.3.1.3. Enter the methods
          4. 6.3.1.4. Enter the @end compiler directive
        2. 6.3.2. Scoping instance variables
      4. 6.4. The Implementation — Coding the Methods
        1. 6.4.1. The implementation compiler directive
        2. 6.4.2. Defining the createBudget: method
        3. 6.4.3. Defining the rest of the methods
        4. 6.4.4. Entering the @end compiler directive
      5. 6.5. Exploring the Program Logic
        1. 6.5.1. Coding the functionality in the main function
          1. 6.5.1.1. Declaring the local variables
          2. 6.5.1.2. Instantiating an object
          3. 6.5.1.3. Sending messages to your objects
          4. 6.5.1.4. Adding the code for England
        2. 6.5.2. Building and running the application
        3. 6.5.3. Extending the program
      6. 6.6. Spreading the Wealth across Files
      7. 6.7. Knowing the Naming Conventions
      8. 6.8. Using id and nil
    4. 7. Objects Objects Everywhere
      1. 7.1. Replacing Numbers with Objects
        1. 7.1.1. Revisiting the documentation
        2. 7.1.2. Using an NSNumber as an argument
          1. 7.1.2.1. Modifying Budget
          2. 7.1.2.2. Modifying main
      2. 7.2. Taking Advantage of Array Objects
        1. 7.2.1. Container classes
        2. 7.2.2. Tiptoeing through an array
        3. 7.2.3. Adding mutable arrays
        4. 7.2.4. Working with fixed arrays
      3. 7.3. Using C Arrays
        1. 7.3.1. Expanding to multidimensional arrays
        2. 7.3.2. Finishing up with the main function
    5. 8. Using the Debugger
      1. 8.1. Identifying the Usual Suspects
        1. 8.1.1. Catching syntax errors
        2. 8.1.2. Crashing with runtime errors
        3. 8.1.3. Dealing with logic errors
      2. 8.2. Using the Debugger
      3. 8.3. Using Breakpoints
      4. 8.4. Using the Static Analyzer
    6. 9. Using Control Statements and Loops
      1. 9.1. Creating Transaction Objects
        1. 9.1.1. Managing all those objects
        2. 9.1.2. Adding the Transaction class
          1. 9.1.2.1. Adding the Transaction class interface
          2. 9.1.2.2. Adding the Transaction class implementation
      2. 9.2. Using switch Statements
      3. 9.3. Using Loop Statements to Repeat Instructions
        1. 9.3.1. The for loop
        2. 9.3.2. The while loop
        3. 9.3.3. The do while loop
      4. 9.4. Adding Loops to Your Program
      5. 9.5. Building the New Application
      6. 9.6. Taking the Leap: Jump Statements
      7. 9.7. Knowing the Problems with switch Statements
  8. III. Walking the Object-Oriented Walk
    1. 10. Basic Inheritance
      1. 10.1. Replacing a Control Structure With Polymorphism
        1. 10.1.1. How inheritance works
        2. 10.1.2. Knowing what inheritance enables you to do
        3. 10.1.3. Using inheritance effectively
      2. 10.2. Implementing Inheritance in a Program
        1. 10.2.1. Creating the Transaction superclass
        2. 10.2.2. Adding the files for the new subclasses
        3. 10.2.3. Implementing the new subclasses
        4. 10.2.4. Modifying main to use the new classes
      3. 10.3. Considering Polymorphism and Inheritance
    2. 11. Encapsulating Objects
      1. 11.1. Getting to Know the Model-View-Controller (MVC) Pattern
      2. 11.2. Implementing the MVC Pattern
        1. 11.2.1. Get out of/into main
          1. 11.2.1.1. Get out of main
          2. 11.2.1.2. Get into main
      3. 11.3. Creating a New Project
      4. 11.4. Creating the Destination Class
        1. 11.4.1. Designing the destination
        2. 11.4.2. Implementing the methods
        3. 11.4.3. Modifying the Budget class
        4. 11.4.4. Removing UI type functionality from the Transaction objects
      5. 11.5. Coding the New main
      6. 11.6. Yes, Another Two Steps Forward and One Step Back
    3. 12. The Birth of an Object
      1. 12.1. Allocating Objects
      2. 12.2. Initializing Objects
        1. 12.2.1. Invoking the superclass's init method
        2. 12.2.2. Initializing instance variables
        3. 12.2.3. Returning back self
      3. 12.3. The Designated Initializer
    4. 13. Getting a Handle on Memory Management
      1. 13.1. Raising and Terminating Responsible Objects
        1. 13.1.1. Understanding the object life cycle
        2. 13.1.2. Using reference counting
      2. 13.2. Running the Static Analyzer
      3. 13.3. Plugging the Leaks
      4. 13.4. Attending to Memory Management Subtleties— Arrays and Autorelease
        1. 13.4.1. Considering objects in arrays
        2. 13.4.2. Understanding autorelease
        3. 13.4.3. Using the autorelease pool
      5. 13.5. Garbage Collection — Taking the Easy Way Out
      6. 13.6. Some Basic Memory Management Rules You Shouldn't Forget
  9. IV. Moving from Language to Application
    1. 14. Getting Data from Other Objects
      1. 14.1. Getting Data from Objects
      2. 14.2. Working with Declared Properties
        1. 14.2.1. Adding properties
        2. 14.2.2. Implementing declared properties
        3. 14.2.3. Accessing the instance variables from within the class
        4. 14.2.4. Releasing the object assigned to a property
      3. 14.3. Using Accessors to Get Data from Objects
      4. 14.4. Properly Using Properties
    2. 15. Show Me the Data
      1. 15.1. Understanding Application-Based Data
        1. 15.1.1. Defining property lists
        2. 15.1.2. Working with property lists
      2. 15.2. Using Dictionaries
        1. 15.2.1. Understanding a dictionary's keys and values
        2. 15.2.2. Creating a dictionary
      3. 15.3. Adding a plist to Your Project
        1. 15.3.1. Using plists
        2. 15.3.2. Creating a mutable dictionary
        3. 15.3.3. Creating, initializing, and using the symbol string
      4. 15.4. Dictionaries of Dictionaries
        1. 15.4.1. Creating a more complex plist
        2. 15.4.2. Managing a dictionary of dictionaries
      5. 15.5. Modifying the plist
        1. 15.5.1. Adding a new entry to the plist
          1. 15.5.1.1. Checking to see if the dictionary is there
          2. 15.5.1.2. Creating the new entry if it's not there
          3. 15.5.1.3. Getting the data stored in the dictionary if it's there
        2. 15.5.2. Updating the dictionary
      6. 15.6. Saving Data in a Separate File
      7. 15.7. Saving Objects as Objects
    3. 16. Extending the Behavior of Objects
      1. 16.1. Understanding Delegation
      2. 16.2. Using Protocols
        1. 16.2.1. Declaring a protocol
        2. 16.2.2. Adopting a protocol
        3. 16.2.3. Adding delegation to Transaction
      3. 16.3. Categories
        1. 16.3.1. Using categories
        2. 16.3.2. Defining informal protocols
    4. 17. Adding an iPhone User Interface
      1. 17.1. Creating Your Project
      2. 17.2. Using Interface Builder to Create a User Interface
      3. 17.3. Implementing the User Interface in Code
        1. 17.3.1. Adding outlets
        2. 17.3.2. Implementing Target-Action
        3. 17.3.3. Adding the methods
      4. 17.4. Connecting Everything Up in Interface Builder and Running iVacation in the Simulator
      5. 17.5. A Final Note
    5. 18. Adding a Mac User Interface
      1. 18.1. Creating Your Project
      2. 18.2. Using Interface Builder to Create a User Interface
      3. 18.3. Implementing the User Interface in Code
        1. 18.3.1. Adding outlets, Target-Action, and the methods
      4. 18.4. Connecting Everything in Interface Builder and Running mVacation on the Mac
      5. 18.5. Knowing What's Left to Do
      6. 18.6. The End of the Beginning
  10. V. The Part of Tens
    1. 19. Ten Debugging Tips
      1. 19.1. Check for Semicolons
      2. 19.2. "Right" Is Not Always "right"
      3. 19.3. When You've Blown It, You've Blown It
      4. 19.4. Compiler Warnings Are for Your Own Good
      5. 19.5. Don't Forget about Memory Errors
      6. 19.6. Get Friendly with Your Debugger
      7. 19.7. Messages to nil
      8. 19.8. Dialing a Wrong Number
      9. 19.9. Create a "Paper" Trail
      10. 19.10. Incrementally Test
      11. 19.11. Use Your Brain
    2. 20. Ten Ways to Be a Happy Developer
      1. 20.1. Keep Everyone in the Dark
      2. 20.2. Make Your Code Easy to Understand
      3. 20.3. Remember Memory
      4. 20.4. Start by Initializing the Right Way
      5. 20.5. Take Advantage of the Documentation
      6. 20.6. Code Code Code
      7. 20.7. Understand that Development Is Not Linear
      8. 20.8. Do It Right from the Start if You Need to Do It Right from the Start
      9. 20.9. Avoid the Code Slinger Mentality
      10. 20.10. The Software Isn't Finished until the Last User Is Dead
      11. 20.11. Keep It Fun
    3. A. About the CD
      1. A.1. On the CD
      2. A.2. System Requirements
      3. A.3. Using the CD
      4. A.4. What You'll Find on the CD
      5. A.5. Troubleshooting
      6. A.6. Customer Care