Learn Objective-C for Java Developers

Book description

Learn Objective-C for Java Developers will guide experienced Java developers into the world of Objective-C. It will show them how to take their existing language knowledge and design patterns and transfer that experience to Objective-C and the Cocoa runtime library. This is the express train to productivity for every Java developer who dreamt of developing for Mac OS X or iPhone, but felt that Objective-C was too intimidating. So hop on and enjoy the ride!

  • Provides a translation service that turns Java problem-solving skills into Objective-C solutions

  • Allows Java developers to leverage their existing experience and quickly launch themselves into a new domain

  • Takes the risk out of learning Objective-C

  • What you'll learn

  • Apply Java experience to Objective-C and Cocoa

  • Use elegant alternatives that increase productivity

  • Maximize the powerfully unique constructs of Objective-C, like class clusters

  • Think like an object-oriented C programmer to create more reusable code

  • Use all of the things in Java and Objective-C that are actually quite similar, like MVC design patterns

  • Learn how to do all of it within Apple's powerful Xcode programming environment using Cocoa frameworks

  • Who is this book for?

    Experienced Java developers interested in developing native applications for Apple's Mac OS X operating system, iPhone, and iPod touch.

    Table of contents

    1. Copyright
    2. About the Author
    3. About the Technical Reviewer
    4. Acknowledgements
    5. Introduction
      1. Who This Book Is For
      2. How This Book Is Structured
      3. Prerequisites
      4. Downloading the Code
      5. Contacting the Author
    6. 1. Language
      1. 1. Introduction
        1. 1.1. What is Objective-C?
          1. 1.1.1. History
          2. 1.1.2. A Modern Object-Oriented Language
          3. 1.1.3. State of the Art Compiler
          4. 1.1.4. Performance
          5. 1.1.5. Dynamism
          6. 1.1.6. Developer Productivity
        2. 1.2. Learning a New Language
        3. 1.3. Terminology and Culture Shock
        4. 1.4. Defining Better
        5. 1.5. Summary
      2. 2. Java and C: Key Differences
        1. 2.1. Primitive Types
        2. 2.2. Constants
        3. 2.3. Typedefs
        4. 2.4. Pointers
        5. 2.5. Structures
        6. 2.6. Object References
        7. 2.7. Arrays
        8. 2.8. static
        9. 2.9. Functions
        10. 2.10. extern
        11. 2.11. Preprocessor
          1. 2.11.1. #include and #import
          2. 2.11.2. #define
          3. 2.11.3. #if
        12. 2.12. Initializing Automatic Variables
        13. 2.13. Labels: break, continue, and goto
        14. 2.14. Summary
      3. 3. Welcome to Objective-C
        1. 3.1. Defining an Objective-C Class
        2. 3.2. Object Pointers
        3. 3.3. Sending Messages
        4. 3.4. Naming Methods
        5. 3.5. Parameter and Return Types
        6. 3.6. Method Selectors
        7. 3.7. Instance Variables
          1. 3.7.1. isa
          2. 3.7.2. Properties
          3. 3.7.3. Property Attributes
        8. 3.8. Overriding Properties
        9. 3.9. Accessing Properties
        10. 3.10. Scope
          1. 3.10.1. Class Name Scope
          2. 3.10.2. Instance Variable Scope
          3. 3.10.3. Method Scope
        11. 3.11. Forward @class Directive
        12. 3.12. self and super
        13. 3.13. Class Methods
        14. 3.14. Constructing Objects
          1. 3.14.1. Writing an init Method
          2. 3.14.2. Chaining Initializers
          3. 3.14.3. Designated Initializer
          4. 3.14.4. Convenience Constructors
        15. 3.15. Destructors
        16. 3.16. What's Missing?
      4. 4. Creating an Xcode Project
        1. 4.1. Download the Project
        2. 4.2. Creating a Project
        3. 4.3. Getting Started
        4. 4.4. Designing the Application
          1. 4.4.1. Designing the User Interface
          2. 4.4.2. Adding a Controller
          3. 4.4.3. Making a Binding
            1. 4.4.3.1. KVC
            2. 4.4.3.2. KVO
            3. 4.4.3.3. Controllers
            4. 4.4.3.4. Bindings
          4. 4.4.4. Adding an Array Controller
        5. 4.5. Getting Down to Business
        6. 4.6. Debugging Your Application
        7. 4.7. Creating Sandbox Applications
        8. 4.8. Summary
      5. 5. Exploring Protocols and Categories
        1. 5.1. Protocols
        2. 5.2. Informal Protocol
        3. 5.3. Combining Formal and Informal Protocols
        4. 5.4. Categories
          1. 5.4.1. Using Categories for Organization
          2. 5.4.2. Hiding Methods
          3. 5.4.3. Augmenting Foreign Classes
          4. 5.4.4. Extensions
        5. 5.5. Summary
      6. 6. Sending Messages
        1. 6.1. Compiling Messages
          1. 6.1.1. Undeclared Methods
          2. 6.1.2. Ambiguous Methods
          3. 6.1.3. Coercion
        2. 6.2. Sending Messages Programmatically
          1. 6.2.1. Immediate Messages
          2. 6.2.2. Deferred Messages
        3. 6.3. Object-Oriented Method Invocation
        4. 6.4. Calling Methods Directly
        5. 6.5. Variable Arguments
        6. 6.6. Unimplemented Methods
        7. 6.7. Summary
      7. 7. Making Friends with nil
        1. 7.1. Messages to nil Are Safe
        2. 7.2. nil Returns Zero
        3. 7.3. Designing With nil
          1. 7.3.1. Property Accessors
          2. 7.3.2. Absent Behavior
          3. 7.3.3. Consistency With Nothing
        4. 7.4. No Free Rides
        5. 7.5. Summary
    7. 2. Translating Technologies
      1. 8. Strings and Primitive Values
        1. 8.1. Wrapping Scalar Primitives
          1. 8.1.1. Scalar Type Conversion
          2. 8.1.2. Converting Strings to Scalars
        2. 8.2. Wrapping Arrays
        3. 8.3. Wrapping Arbitrary Values
        4. 8.4. Wrapping nil
        5. 8.5. Strings
          1. 8.5.1. Converting Objects to Strings
          2. 8.5.2. C Strings
            1. 8.5.2.1.
              1. 8.5.2.1.1. Converting C Strings into NSString Objects
              2. 8.5.2.1.2. Converting NSString Objects into C Strings
        6. 8.6. Formatting Strings
          1. 8.6.1. NSFormatter
            1. 8.6.1.1. NSNumberFormatter
            2. 8.6.1.2. NSDateFormatter
        7. 8.7. Summary
      2. 9. Garbage Collection
        1. 9.1. Choosing to Use Garbage Collection
        2. 9.2. Writing Code with Garbage Collection
        3. 9.3. Writing Finalize Methods
        4. 9.4. Creating Weak References
        5. 9.5. Creating Strong References
        6. 9.6. Encouraging Garbage Collection
        7. 9.7. GC vs. Non-GC Pointers
          1. 9.7.1. Write Barriers
          2. 9.7.2. Allocating Collectable Memory
          3. 9.7.3. Garbage Collection Pitfalls
            1. 9.7.3.1. Interior Pointers
            2. 9.7.3.2. Opaque Pointers
            3. 9.7.3.3. Enumerating Weak Collections
            4. 9.7.3.4. Uninitialized Stack References
            5. 9.7.3.5. Other Pitfalls
        8. 9.8. Design Patterns to Avoid
        9. 9.9. Debugging
        10. 9.10. Summary
      3. 10. Introspection
        1. 10.1. Testing for Methods
        2. 10.2. Testing Class Membership
        3. 10.3. Key-Value Coding
          1. 10.3.1. Using Key-Value Coding
          2. 10.3.2. Designing KVC-Compliant Classes
          3. 10.3.3. Custom Key Values
        4. 10.4. Inspecting Classes
        5. 10.5. Exploring Protocols
        6. 10.6. Exploring Methods
        7. 10.7. Exploring Properties
        8. 10.8. Exploring Instance Variables
        9. 10.9. Summary
      4. 11. Files
        1. 11.1. File System APIs
        2. 11.2. Identifying Items in the File System
          1. 11.2.1. File and Path Names
          2. 11.2.2. Working Directory
          3. 11.2.3. File URLs
          4. 11.2.4. Creating and Deleting Directories
          5. 11.2.5. Locating Special Directories
        3. 11.3. Requesting a File from the User
        4. 11.4. Symbolic Links, Hard Links, and Aliases
        5. 11.5. Working With the Contents of a Directory
        6. 11.6. File Properties
        7. 11.7. High-Level File Operations
        8. 11.8. NSWorkspace
        9. 11.9. Random File Access
        10. 11.10. NSFileManager Delegate
        11. 11.11. Alternate APIs
        12. 11.12. Summary
      5. 12. Serialization
        1. 12.1. Archiving
          1. 12.1.1. Archive Types
          2. 12.1.2. Archive Coders
          3. 12.1.3. Archives and Documents
          4. 12.1.4. Adding Keyed Archive Support to Your Class
          5. 12.1.5. Adding Sequential Archive Support to Your Class
          6. 12.1.6. Supporting Both Keyed and Sequential Archiving
          7. 12.1.7. Archiving Complications
            1. 12.1.7.1. Transient Properties
            2. 12.1.7.2. Duplicate Objects
            3. 12.1.7.3. Limiting the Object Graph
            4. 12.1.7.4. Class Version Compatibility
              1. 12.1.7.4.1. Forward and Backward Compatibility in Keyed Archives
              2. 12.1.7.4.2. Backward Compatibility in Sequential Archives
              3. 12.1.7.4.3. Class Replacement
              4. 12.1.7.4.4. Class Substitution During Decoding
            5. 12.1.7.5. Class Substitution During Encoding
        2. 12.2. Objective-C Serialization
          1. 12.2.1. Property Lists
          2. 12.2.2. XML
        3. 12.3. Copying Objects
        4. 12.4. Summary
      6. 13. Communicating Near and Far
        1. 13.1. Communicating Within a Single Process
        2. 13.2. Communicating with Other Processes
          1. 13.2.1. Low-Level Communications
            1. 13.2.1.1. NSPort
            2. 13.2.1.2. NSPipe
            3. 13.2.1.3. NSFileHandle
            4. 13.2.1.4. NSStream
          2. 13.2.2. High-Level Communications
            1. 13.2.2.1. Distributed Notifications
            2. 13.2.2.2. Distributed Objects
              1. 13.2.2.2.1. How Distributed Objects Works
              2. 13.2.2.2.2. Making a Connection
            3. 13.2.2.3. Inter-thread Connection
            4. 13.2.2.4. Mach Ports Connection
            5. 13.2.2.5. Network Connection
            6. 13.2.2.6. BSD Pipe Connection
              1. 13.2.2.6.1. Run Loops
              2. 13.2.2.6.2. Asynchronous Messages
              3. 13.2.2.6.3. Passing Objects by Copy
            7. 13.2.2.7. Passing Pointers
              1. 13.2.2.7.1. Is it an Object or a Proxy?
        3. 13.3. Networking
          1. 13.3.1. Network Services
          2. 13.3.2. URL Loading
            1. 13.3.2.1. Trivial URL Request
            2. 13.3.2.2. Asynchronous URL Request
            3. 13.3.2.3. Writing to a URL
            4. 13.3.2.4. Downloading a URL
            5. 13.3.2.5. Caches and Cookies
        4. 13.4. Summary
      7. 14. Exception Handling
        1. 14.1. Using Exceptions
        2. 14.2. Exception Handling Differences
          1. 14.2.1. No Catch or Specify
          2. 14.2.2. Throw Any Object
          3. 14.2.3. Re-Throw an Exception
          4. 14.2.4. Catch Order
          5. 14.2.5. Chaining
          6. 14.2.6. Call Stack
          7. 14.2.7. Performance
          8. 14.2.8. Uncaught Exceptions
        3. 14.3. Legacy Exceptions
        4. 14.4. Assertions
        5. 14.5. Alternatives to Exceptions
          1. 14.5.1. Simple Errors
          2. 14.5.2. POSIX Error Codes
          3. 14.5.3. Core Foundation Error Codes
          4. 14.5.4. Cocoa Errors
            1. 14.5.4.1. Error Domains
            2. 14.5.4.2. Customization and Display
            3. 14.5.4.3. Localization
            4. 14.5.4.4. Recovery
        6. 14.6. Combining Errors and Exceptions
        7. 14.7. Summary
      8. 15. Threads
        1. 15.1. Thread API
        2. 15.2. Starting a Thread
        3. 15.3. Managing Threads
          1. 15.3.1. Putting a Thread to Sleep
          2. 15.3.2. Thread Properties
            1. 15.3.2.1. Information
        4. 15.4. Thread-Specific Values
          1. 15.4.1.
            1. 15.4.1.1. Priority
            2. 15.4.1.2. Stack Size
            3. 15.4.1.3. Name
        5. 15.5. Terminating a Thread
        6. 15.6. Run Loops
          1. 15.6.1. Starting a Run Loop
          2. 15.6.2. Run Loop Modes
          3. 15.6.3. Stopping a Run Loop
          4. 15.6.4. Customizing Run Loops
        7. 15.7. Thread Notifications
        8. 15.8. Thread Synchronization
          1. 15.8.1. The @synchronize Directive
          2. 15.8.2. Mutual Exclusion Semaphore Objects
            1. 15.8.2.1. NSRecursiveLock
            2. 15.8.2.2. NSLock
            3. 15.8.2.3. NSConditionLock
            4. 15.8.2.4. NSDistributedLock
          3. 15.8.3. Spin Locks
        9. 15.9. Operations
        10. 15.10. Timers
        11. 15.11. Summary
    8. 3. Programming Patterns
      1. 16. Collection Patterns
        1. 16.1. Immutable Collections
        2. 16.2. Ordered Collections
          1. 16.2.1. Common Methods
          2. 16.2.2. NSArray, NSMutableArray
          3. 16.2.3. NSPointerArray
        3. 16.3. Dictionary Collections
          1. 16.3.1. Common Methods
          2. 16.3.2. NSDictionary, NSMutableDictionary
          3. 16.3.3. NSMapTable
        4. 16.4. Set Collections
          1. 16.4.1. Common Methods
          2. 16.4.2. NSSet, NSMutableSet
          3. 16.4.3. NSCountedSet
          4. 16.4.4. NSIndexSet
          5. 16.4.5. NSHashTable
        5. 16.5. Composite Pattern
        6. 16.6. Collection Equality Contracts
        7. 16.7. Comparing Collections
        8. 16.8. Iterator Pattern
          1. 16.8.1. Using Fast Enumeration
          2. 16.8.2. Using Enumerators
          3. 16.8.3. Addressing Collection Objects
          4. 16.8.4. Adding Enumeration Support
        9. 16.9. Sorting Collections
          1. 16.9.1. Objective-C Message Sorting
          2. 16.9.2. C Function Sorting
          3. 16.9.3. Sort Descriptors
        10. 16.10. Filtering Collections
        11. 16.11. Collection Concurrency
          1. 16.11.1. Enumerate a Copy of the Collection
          2. 16.11.2. Defer Changes to the Collection
          3. 16.11.3. Thread Safety
          4. 16.11.4. Garbage Collection and Weak Collections
        12. 16.12. Summary
      2. 17. Delegation Pattern
        1. 17.1. Understanding Delegates
        2. 17.2. Using Delegates
        3. 17.3. Delegate Methods
        4. 17.4. Delegate Protocols
        5. 17.5. Incorporating the Delegation Pattern
        6. 17.6. Summary
      3. 18. Provider/Subscriber Pattern
        1. 18.1. Notifications
        2. 18.2. Notification Centers
        3. 18.3. Posting Synchronous Notifications
        4. 18.4. Being a Discriminating Observer
        5. 18.5. Removing an Observer
        6. 18.6. Notification Queuing
          1. 18.6.1. Queuing a Notification
          2. 18.6.2. Coalescing Notifications
          3. 18.6.3. Dequeuing Notifications
        7. 18.7. Distributed Notifications
          1. 18.7.1. Distributed Notifications Center
          2. 18.7.2. Property List Values
          3. 18.7.3. Asynchronous Notification Delivery
          4. 18.7.4. Suspending a Distributed Notification Center
        8. 18.8. Summary
      4. 19. Observer Pattern
        1. 19.1. Key-Value Observing at Work
        2. 19.2. Registering a Key-Value Observer
        3. 19.3. Processing Key-Value Change Notifications
        4. 19.4. Unregistering an Observer
        5. 19.5. Making Your Classes KVO Compliant
          1. 19.5.1. Sending Manual KVO Notifications
          2. 19.5.2. Creating Property Dependencies
          3. 19.5.3. Overriding Key-Value Observing
        6. 19.6. Optimizing Key-Value Observing
        7. 19.7. Summary
      5. 20. Model-View-Controller Pattern
        1. 20.1. Understanding Model-View-Controller
          1. 20.1.1. MVC Variations
            1. 20.1.1.1. Combined Controller and Data Model
            2. 20.1.1.2. Mediating Controller
            3. 20.1.1.3. Direct View and Data Model Binding
            4. 20.1.1.4. Other Variations
          2. 20.1.2. The Advantages of MVC
            1. 20.1.2.1. Modularity
            2. 20.1.2.2. Flexibility
            3. 20.1.2.3. Reuse
            4. 20.1.2.4. Scaling
        2. 20.2. Bindings
        3. 20.3. Interface Builder
          1. 20.3.1. NIB Documents
          2. 20.3.2. The NIB Document Window
          3. 20.3.3. Object Properties
          4. 20.3.4. Placeholder Objects
          5. 20.3.5. Connections
            1. 20.3.5.1. Outlets
            2. 20.3.5.2. Actions
            3. 20.3.5.3. Bindings
          6. 20.3.6. Owner Object
          7. 20.3.7. Custom Objects
          8. 20.3.8. Object Instantiation
          9. 20.3.9. NIB Object Initialization
        4. 20.4. Views
          1. 20.4.1. View Geometry
            1. 20.4.1.1. Coordinate Points
            2. 20.4.1.2. Coordinate System
            3. 20.4.1.3. Pen Orientation
            4. 20.4.1.4. Drawing Bounds
            5. 20.4.1.5. Drawing Lines and Shapes
          2. 20.4.2. Custom Views
            1. 20.4.2.1. Invalidating and Drawing Views
            2. 20.4.2.2. Graphics Context
            3. 20.4.2.3. The Graphics Context State Stack
            4. 20.4.2.4. Drawing Tools
            5. 20.4.2.5. Animation
            6. 20.4.2.6. iPhone View Classes
            7. 20.4.2.7. Advanced View Topics
        5. 20.5. Document Model
        6. 20.6. Events and Responders
          1. 20.6.1. The Dynamic Application
          2. 20.6.2. Events
          3. 20.6.3. Event Objects
          4. 20.6.4. Key Events
          5. 20.6.5. Mouse Events
            1. 20.6.5.1. Mouse Down Event
            2. 20.6.5.2. Mouse Drag and Mouse Up Events
            3. 20.6.5.3. Mouse Tracking
          6. 20.6.6. The Responder Chain
            1. 20.6.6.1. Action Messages
            2. 20.6.6.2. Sending Action Messages
            3. 20.6.6.3. Menu Actions
            4. 20.6.6.4. Disabling Action Menu Items
            5. 20.6.6.5. Designing with the Responder Chain
        7. 20.7. Data Models
          1. 20.7.1. Legacy Table and Tree Models
          2. 20.7.2. Collection Controllers
          3. 20.7.3. Core Data
          4. 20.7.4. Custom Data Model Objects
        8. 20.8. Controllers
          1. 20.8.1. Custom Controllers
            1. 20.8.1.1. Creating a Custom NSApplication
            2. 20.8.1.2. Creating a Custom NSDocument
          2. 20.8.2. NSController Controllers
        9. 20.9. About TicTacToe
          1. 20.9.1. Info.plist
          2. 20.9.2. Undo
          3. 20.9.3. Resources
          4. 20.9.4. Localized Resources
        10. 20.10. Summary
      6. 21. Lazy Initialization Pattern
        1. 21.1. Implementing the Pattern
        2. 21.2. Lazy Initialization of Global Variables
        3. 21.3. The Class +initialize Method
        4. 21.4. Summary
      7. 22. Factory Pattern
        1. 22.1. URL Factory
        2. 22.2. Matrix Class
          1. 22.2.1. Java Matrix Factory
          2. 22.2.2. Objective-C Matrix Class Cluster
        3. 22.3. Summary
      8. 23. Singleton Pattern
        1. 23.1. Implementing Singletons
        2. 23.2. Lazy Singletons
        3. 23.3. Singleton Factory
        4. 23.4. Summary
    9. 4. Advanced Objective-C
      1. 24. Memory Management
        1. 24.1. C Memory Allocation
        2. 24.2. Objective-C Reference Counting
        3. 24.3. Autorelease Pools
          1. 24.3.1. Autorelease Pool Lifetime
          2. 24.3.2. Returned References
          3. 24.3.3. Autoreleased Objects
        4. 24.4. Managed Memory Patterns
          1. 24.4.1. New Object Patterns
          2. 24.4.2. Autoreleased Object Pattern
          3. 24.4.3. Returning Autoreleased Objects
          4. 24.4.4. Setter Patterns
          5. 24.4.5. init Patterns
          6. 24.4.6. dealloc Patterns
          7. 24.4.7. Implicitly Retained Objects
        5. 24.5. Managed Memory Problems
          1. 24.5.1. Overretained or Underreleased Objects
          2. 24.5.2. Overreleased or Underretained Objects
          3. 24.5.3. Prematurely Released Objects
          4. 24.5.4. Circular References
        6. 24.6. Creating Autorelease Pools
        7. 24.7. Mixing Managed Memory and Garbage Collection
        8. 24.8. Summary
      2. 25. Mixing C and Objective-C
        1. 25.1. Using C in Objective-C
          1. 25.1.1. Calling C Functions from Objective-C
          2. 25.1.2. Using Objective-C Objects in C
        2. 25.2. Core Foundation
          1. 25.2.1. The Toll-Free Bridge
          2. 25.2.2. C Memory Management
            1. 25.2.2.1. Using Core Foundation Memory Management Patterns
            2. 25.2.2.2. Using Core Foundation with Garbage Collection
            3. 25.2.2.3. Using Core Foundation with Managed Memory
        3. 25.3. Summary
      3. 26. Runtime
        1. 26.1. Process
          1. 26.1.1. Environment
          2. 26.1.2. Command-Line Arguments
          3. 26.1.3. Process Attributes
          4. 26.1.4. Version
            1. 26.1.4.1. Controlling Development and Deployment Versions
            2. 26.1.4.2. Testing for Classes, Methods, and Functions
        2. 26.2. Packages and Bundles
        3. 26.3. Frameworks
        4. 26.4. User Defaults
        5. 26.5. isa Swizzling
        6. 26.6. 64-Bit Programming
        7. 26.7. Summary
        8. 26.8. Epilogue

    Product information

    • Title: Learn Objective-C for Java Developers
    • Author(s): James Bucanek
    • Release date: September 2009
    • Publisher(s): Apress
    • ISBN: 9781430223696