You are previewing Programming in Objective-C, Fifth Edition.

Programming in Objective-C, Fifth Edition

Cover of Programming in Objective-C, Fifth Edition by Stephen G. Kochan Published by Addison-Wesley Professional
  1. Title Page
  2. Copyright Page
  3. Dedication Page
  4. Contents at a Glance
  5. Table of Contents
  6. Developer’s Library
  7. About the Author
  8. About the Technical Reviewers
  9. We Want to Hear from You!
  10. Reader Services
  11. 1. Introduction
    1. What You Will Learn from This Book
    2. How This Book Is Organized
    3. Support
    4. Acknowledgments
    5. Preface to the Fifth Edition
  12. Part I: The Objective-C Language
    1. 2. Programming in Objective-C
      1. Compiling and Running Programs
      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
      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
      2. Arithmetic Expressions
      3. Assignment Operators
      4. A Calculator Class
      5. Exercises
    4. 5. Program Looping
      1. The for Statement
      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
      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
      5. Local Variables
      6. The self Keyword
      7. Allocating and Returning Objects from Methods
      8. Exercises
    7. 8. Inheritance
      1. It All Begins at the Root
      2. Extension through Inheritance: Adding New Methods
      3. Overriding Methods
      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
      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
      3. Enumerated Data Types
      4. The typedef Statement
      5. Data Type Conversions
      6. Bit Operators
      7. Exercises
    10. 11. Categories and Protocols
      1. Categories
      2. Class Extensions
      3. Protocols and Delegation
      4. Composite Objects
      5. Exercises
    11. 12. The Preprocessor
      1. The #define Statement
      2. The #import Statement
      3. Conditional Compilation
      4. Exercises
    12. 13. Underlying C Language Features
      1. Arrays
      2. Functions
      3. Blocks
      4. Structures
      5. Pointers
      6. They’re Not Objects!
      7. Miscellaneous Language Features
      8. How Things Work
      9. Exercises
  13. Part 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
      3. Array Objects
      4. Dictionary Objects
      5. Set Objects
      6. Exercises
    3. 16. Working with Files
      1. Managing Files and Directories: NSFileManager
      2. Working with Paths: NSPathUtilities.h
      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
      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
  14. Part 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
      3. An iPhone Fraction Calculator
      4. Summary
      5. Exercises
  15. Appendixes
    1. Appendix A. Glossary
    2. Appendix B. Address Book Example Source Code
  16. Index
  17. Ad Page
O'Reilly logo

11. Categories and Protocols

In this chapter, you learn how to add methods to a class in a modular fashion through the use of categories and how to create a standardized list of methods for others to implement.

Categories

Sometimes you might be working with a class definition and want to add some new methods to it. For example, you might decide for your Fraction class that, in addition to the add: method for adding two fractions, you want to have methods to subtract, multiply, and divide two fractions.

As another example, suppose that you are working on a large programming project and, as part of that project, your group is defining a new class that contains many different methods. You have been assigned the task of writing methods for the class ...

The best content for your career. Discover unlimited learning on demand for around $1/day.