You are previewing Mac Programming for Absolute Beginners.
O'Reilly logo
Mac Programming for Absolute Beginners

Book Description

Want to learn how to program on your Mac? Not sure where to begin? Best-selling author Wallace Wang will explain how to get started with Cocoa, Objective-C, and Xcode. Whether you are an experienced Windows coder moving to the Mac, or you are completely new to programming, you’ll see how the basic design of a Mac OS X program works, how Objective-C differs from other languages you may have used, and how to use the Xcode development environment. Most importantly, you’ll learn how to use elements of the Cocoa framework to create windows, store data, and respond to users in your own Mac programs. If you want to learn how to develop apps with Cocoa, Objective-C, and Xcode, this book is a great first step.

Here are just a few of the things you’ll master along the way:

  • Fundamental programming concepts aided by short, easy-to-understand examples

  • How to use Xcode and related programming tools to save time and work more efficiently

  • A firm understanding of the basics of Objective-C and how it compares to other languages you might know

  • How to create simple apps using the Cocoa framework

  • How to easily design, write, test, and market your finished program

With this book and your trusty Mac, you’re well on your way to transforming your Mac app ideas into real applications.

What you'll learn

  • Basic programming concepts illustrated with short, easy-to-understand examples

  • How to use Xcode and related programming tools

  • The basics of Objective-C and how it compares to other languages you might know

  • How to create simple apps using the Cocoa framework

  • What you'll learn

    Mac users who want to learn programming or Windows developers who are moving to the Mac OS and iOS platforms. No previous programming experience required.

Table of Contents

  1. Copyright
  2. About the Author
  3. About the Technical Reviewer
  4. Acknowledgments
  5. Introduction
    1. Code Conventions Used in This Book
    2. What to Expect from This Book
  6. 1. Understanding Programming
    1. 1.1. Programming Principles
      1. 1.1.1. Dividing Programs into Parts
      2. 1.1.2. Event-Driven Programming
      3. 1.1.3. Object-Oriented Programming
        1. 1.1.3.1. Encapsulation
        2. 1.1.3.2. Polymorphism
        3. 1.1.3.3. Inheritance
    2. 1.2. Understanding Programming Languages
      1. 1.2.1. The Building Blocks of Programming Languages
      2. 1.2.2. ProgrammingFrameworks
      3. 1.2.3. Mac Programming Today
    3. 1.3. Summary
  7. 2. Understanding Apple's Programming Tools
    1. 2.1. Understanding Editors
    2. 2.2. Understanding Xcode
      1. 2.2.1. Deciphering the Xcode User Interface
      2. 2.2.2. Running Xcode
      3. 2.2.3. Creating a New Project in Xcode
      4. 2.2.4. Examining Project Files in Xcode
      5. 2.2.5. Compiling a Program
    3. 2.3. Summary
  8. 3. The Basic Steps to Creating a Mac Program
    1. 3.1. A Bare-Bones Program Example
    2. 3.2. A Simple User Interface Example
    3. 3.3. An Interactive User Interface Example
      1. 3.3.1. Writing Objective-C Code
        1. 3.3.1.1. Creating an Action Method
      2. 3.3.2. Connecting the User Interface
    4. 3.4. An Advanced Interactive User Interface Example
    5. 3.5. Summary
  9. 4. Getting Help
    1. 4.1. Installing Help Topics
    2. 4.2. Getting Help About Xcode
    3. 4.3. Getting Help About Core Library
    4. 4.4. Searching for Help
    5. 4.5. Getting Quick Help
    6. 4.6. Viewing Documentation for Selected Text
    7. 4.7. Getting Help with Library Windows
    8. 4.8. Help While Writing Code
      1. 4.8.1. Color-Coding
      2. 4.8.2. Customizing the Editor
      3. 4.8.3. Using Code Completion
    9. 4.9. Summary
  10. 5. Learning Objective-C
    1. 5.1. Differences in Writing a Mac Objective-C Program
    2. 5.2. Understanding Objective-C Symbols
      1. 5.2.1. Defining the End of Each Line with a Semicolon
      2. 5.2.2. Defining the Beginning and End of Code with Curly Brackets
      3. 5.2.3. Defining Compiler Directives with the # Symbol
      4. 5.2.4. Defining Comments with //
      5. 5.2.5. Identifying Objects with [ and ]
      6. 5.2.6. Defining Pointers with *
    3. 5.3. Manipulating Data with Variables
      1. 5.3.1. Declaring Variables
      2. 5.3.2. Assigning Data to a Variable
      3. 5.3.3. The Scope of a Variable
      4. 5.3.4. A Program Example Using Variables
    4. 5.4. Using Constants
    5. 5.5. Using Mathematical Operators
    6. 5.6. Using Strings
    7. 5.7. Summary
  11. 6. Making Decisions with Branches
    1. 6.1. Understanding Boolean Expressions
      1. 6.1.1. Boolean Comparison Operators
      2. 6.1.2. Boolean Logical Operators
        1. 6.1.2.1. The ! (Not) Operator
        2. 6.1.2.2. The && (And) Operator
        3. 6.1.2.3. The || (Or) Operator
        4. 6.1.2.4. The ^ (Xor) Operator
    2. 6.2. Branches
      1. 6.2.1. The Simplest if Statement
      2. 6.2.2. Following Multiple Instructions in an if Statement
      3. 6.2.3. The if-else Statement
      4. 6.2.4. The if-else if Statement
      5. 6.2.5. The switch Statement
    3. 6.3. Summary
  12. 7. Repeating Code with Loops
    1. 7.1. Loops That Run a Fixed Number of Times
      1. 7.1.1. Quitting a for Loop Prematurely
      2. 7.1.2. Skipping in a for Loop
    2. 7.2. Loops That Run Zero or More Times
      1. 7.2.1. The while Loop
      2. 7.2.2. The do-while Loop
      3. 7.2.3. Quitting a while or do-while Loop Prematurely
      4. 7.2.4. Skipping a while or do-while Loop
    3. 7.3. Nested Loops
    4. 7.4. Summary
  13. 8. Understanding the Cocoa Framework
    1. 8.1. An Overview of How Object-Oriented Programming Works
      1. 8.1.1. Starting with a Class
      2. 8.1.2. Reducing Bugs
      3. 8.1.3. Reusing Code
      4. 8.1.4. Defining Classes
    2. 8.2. Creating an Object
    3. 8.3. Storing Data in an Object
    4. 8.4. A Sample Program for Manipulating Objects
    5. 8.5. Looking Up Method and Property Names for NS Classes
    6. 8.6. Summary
  14. 9. Manipulating Strings
    1. 9.1. Declaring a String Variable
    2. 9.2. Getting the Length of a String
    3. 9.3. Comparing Two Strings
    4. 9.4. Checking for Prefixes and Suffixes
    5. 9.5. Converting to Uppercase and Lowercase
    6. 9.6. Converting Strings to Numbers
    7. 9.7. Searching for a Substring
      1. 9.7.1. The location Field
      2. 9.7.2. The length Field
    8. 9.8. Searching and Replacing
      1. 9.8.1. Replacing Part of a String at a Specific Location
      2. 9.8.2. Searching for and Replacing Part of a String
    9. 9.9. Deleting Part of a String
    10. 9.10. Extracting a Substring
      1. 9.10.1. Extracting a Substring with a Location and Length
      2. 9.10.2. Extracting a Substring to the End of a String
    11. 9.11. Appending a Substring
    12. 9.12. Inserting a String
    13. 9.13. Summary
  15. 10. Arrays
    1. 10.1. Creating an Array
      1. 10.1.1. Finding the Right Method to Use
      2. 10.1.2. StoringObjects in an Array
      3. 10.1.3. Additional Methods for Filling an Array
    2. 10.2. Counting the Items Stored in an Array
    3. 10.3. Accessing an Item in an Array
    4. 10.4. Accessing All Items in an Array
    5. 10.5. Adding Items to an Array
    6. 10.6. Inserting Items into an Array
    7. 10.7. Deleting Items from an Array
      1. 10.7.1. Deleting the Last Item in an Array
      2. 10.7.2. Deleting an Item from a Specific Index Position
      3. 10.7.3. Deleting Every Item from an Array
      4. 10.7.4. Deleting All Instances of an Item from an Array
    8. 10.8. Summary
  16. 11. Dictionaries and Sets
    1. 11.1. Dictionary Basics
    2. 11.2. Creating and Putting Data in a Dictionary
    3. 11.3. Counting the Items Stored in a Dictionary
    4. 11.4. Retrieving an Item from a Dictionary
    5. 11.5. Deleting Data from a Dictionary
    6. 11.6. Copying a Dictionary
    7. 11.7. Copying Dictionary Data Into an Array
    8. 11.8. Sorting Keys
    9. 11.9. Access All Items in a Dictionary
    10. 11.10. Using Sets
    11. 11.11. Creating and Putting Data in a Set
    12. 11.12. Counting the Number of Items in a Set
    13. 11.13. Checking Whether Data Is in a Set
    14. 11.14. Adding and Removing Data in a Set
    15. 11.15. Accessing All Items in a Set
    16. 11.16. Getting the Intersection of Two Sets
    17. 11.17. Identifying a Subset of a Set
    18. 11.18. Summary
  17. 12. Creating Classes and Objects
    1. 12.1. Creating a Class
    2. 12.2. Understanding the Code in a Class
    3. 12.3. Deleting Class Files
    4. 12.4. A Program Example of a Class
    5. 12.5. Creating Methods
      1. 12.5.1. Passing Parameters
        1. 12.5.1.1. Passing Multiple Parameters
        2. 12.5.1.2. Passing Objects as Parameters
      2. 12.5.2. Returning Values from a Method
      3. 12.5.3. Passing by Reference
    6. 12.6. Creating Class Properties
      1. 12.6.1. Defining Properties
      2. 12.6.2. Accessing and Getting Values in Properties
    7. 12.7. Summary
  18. 13. Inheritance, Method Overriding, and Events
    1. 13.1. Object Inheritance
    2. 13.2. Method Overriding
    3. 13.3. Responding to Events
      1. 13.3.1. Understanding the Application Delegate
    4. 13.4. Summary
  19. 14. Creating a User Interface
    1. 14.1. Getting to Know Interface Builder
      1. 14.1.1. Creating a New User Interface .xib File
    2. 14.2. Understanding the Parts of a .XIB File
      1. 14.2.1. Placeholder Objects
      2. 14.2.2. Interface Objects
      3. 14.2.3. Toggling the View of Placeholder and Interface Objects
    3. 14.3. Designing a User Interface
    4. 14.4. Customizing User Interface Objects
      1. 14.4.1. Moving and Resizing User Interface Objects
      2. 14.4.2. Autosizing and Anchoring User Interface Objects
    5. 14.5. Summary
  20. 15. Choosing Commands with Buttons
    1. 15.1. Creating a Button
    2. 15.2. Creating a Button Title
    3. 15.3. Adding a Graphic Image
    4. 15.4. Customizing the Visual Behavior of a Button
    5. 15.5. Making Buttons Easier to Use
      1. 15.5.1. Creating Tooltips
      2. 15.5.2. Adding Sound
      3. 15.5.3. Choosing a Button with a Keystroke Combination
    6. 15.6. Connecting a Button to an IBAction
      1. 15.6.1. Alternate Dragging Option
    7. 15.7. Breaking a Link to an IBAction Method
    8. 15.8. Summary
  21. 16. Making Choices with Radio Buttons and Check Boxes
    1. 16.1. Radio Buttons
      1. 16.1.1. Creating and Adding Radio Buttons
      2. 16.1.2. Creating a Radio Button Title
      3. 16.1.3. Defining a Radio Button's State
    2. 16.2. Determining Which Radio Button a User Selected
    3. 16.3. Check Boxes
      1. 16.3.1. Creating Check Boxes
      2. 16.3.2. Defining a Check Box's Title and State
    4. 16.4. Summary
  22. 17. Making Choices with Pop-Up Buttons
    1. 17.1. Pop-Up Button Basics
    2. 17.2. Creating a Pop-Up Button List in Interface Builder
      1. 17.2.1. Adding (and Deleting) Items on a Pop-Up Button List
      2. 17.2.2. Renaming an Item in a Pop-Up Button List
    3. 17.3. Modifying a Pop-Up Button's List with Code
    4. 17.4. Determining What a User Selected
    5. 17.5. Summary
  23. 18. Inputting and Outputting Data with Labels, Text Fields, and Combo Boxes
    1. 18.1. Using Labels
      1. 18.1.1. Adding a Label to Your User Interface
      2. 18.1.2. Editing Text on a Label
    2. 18.2. Using Text Fields
      1. 18.2.1. Adding a Text Field to Your User Interface
      2. 18.2.2. Editing Text in a Text Field
      3. 18.2.3. Retrieving Data from a Text Field
    3. 18.3. Using Combo Boxes
      1. 18.3.1. Adding a Combo Box to Your User Interface
      2. 18.3.2. Creating a List for a Combo Box
      3. 18.3.3. Retrieving a Value from a Combo Box
    4. 18.4. Wrapping Labels and Text Fields
    5. 18.5. Summary
  24. 19. Inputting Data with Sliders, Date Pickers, and Steppers
    1. 19.1. Using Sliders
      1. 19.1.1. Defining Values
      2. 19.1.2. Displaying Tick Marks
      3. 19.1.3. Retrieving and Displaying a Slider's Value
    2. 19.2. Using a Date Picker
      1. 19.2.1. Retrieving a Date from a Date Picker
    3. 19.3. Using Steppers
    4. 19.4. Summary
  25. 20. Using Built-In Dialog Boxes
    1. 20.1. Using Alert Dialog Boxes
      1. 20.1.1. Displaying Text on a Dialog Box
      2. 20.1.2. Displaying a Suppression Check Box
      3. 20.1.3. Displaying Buttons on a Dialog Box
    2. 20.2. Creating an Open Panel
      1. 20.2.1. Limiting File Types
      2. 20.2.2. Allowing Multiple File Selections
    3. 20.3. Creating a Save Panel
      1. 20.3.1. Limiting File Types
    4. 20.4. Summary
  26. 21. Creating Pull-Down Menus
    1. 21.1. Editing Pull-Down Menus
      1. 21.1.1. Editing a Menu or Menu Item
      2. 21.1.2. Moving a Menu or Menu Item
      3. 21.1.3. Deleting Menus and Menu Items
      4. 21.1.4. Creating New Menus and Menu Items
    2. 21.2. Linking Menu Commands
    3. 21.3. Assigning Keystrokes to a Menu Item
    4. 21.4. Summary
  27. 22. Designing Your Own Programs
    1. 22.1. Identifying the Right Problem
    2. 22.2. What Programs Do Well
    3. 22.3. Designing the Program Structure
      1. 22.3.1. The Model
      2. 22.3.2. The Controller
      3. 22.3.3. The View
      4. 22.3.4. Be Conventional
      5. 22.3.5. Be Imitative
      6. 22.3.6. Be Unusual
    4. 22.4. Thinking in Objects
      1. 22.4.1. Picking a Data Structure
      2. 22.4.2. Creating an Algorithm
      3. 22.4.3. Defining an Algorithm
      4. 22.4.4. Writing Pseudocode
      5. 22.4.5. Writing Actual Code
      6. 22.4.6. Prototyping Your Program
      7. 22.4.7. Writing and Testing Your Program
    5. 22.5. Summary
  28. 23. Working with Xcode
    1. 23.1. Creating New Folders
    2. 23.2. Fast Navigation Shortcuts
      1. 23.2.1. Using the File History Pop-Up Button
      2. 23.2.2. Using the Properties and Methods Pop-Up Button
      3. 23.2.3. Using the Classes Menu
      4. 23.2.4. Using the Include Menu
      5. 23.2.5. Switching Between the .h File and .m File
    3. 23.3. Making Code Easier to Read
      1. 23.3.1. Folding (or Unfolding) All Methods and Functions
      2. 23.3.2. Folding (or Unfolding) a Single Block of Code
      3. 23.3.3. Folding (or Unfolding) a Block of Comments
      4. 23.3.4. Unfolding Everything
    4. 23.4. Splitting the Xcode Window
    5. 23.5. Summary
  29. 24. Debugging Your Program
    1. 24.1. Debugging a Program
      1. 24.1.1. Syntax Errors
        1. 24.1.1.1. Using One = Instead of == to Compare Values
        2. 24.1.1.2. Omitting break Statements in a switch Statement
        3. 24.1.1.3. Omitting @ and *
        4. 24.1.1.4. Omitting Matching Pairs in Header (.h) and Implementation (.m) Files
      2. 24.1.2. Logic Errors
      3. 24.1.3. Run-Time Errors
    2. 24.2. Viewing Problems When Debugging
    3. 24.3. Simple Debugging Tips
      1. 24.3.1. Comment Out Your Code
      2. 24.3.2. Check the Value of Variables with NSLog
    4. 24.4. Using Breakpoints When Debugging
      1. 24.4.1. Placing (and Removing) a Breakpoint
      2. 24.4.2. Using the Debugger
      3. 24.4.3. Stepping Through Code
    5. 24.5. Summary