Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Title Page
Dedication
Acknowledgments
Table of Contents
Introduction
About This Book
Prerequisites
Typographical conventions
Using an eBook
What’s new in the fifth edition?
The Story of Cocoa
NeXTSTEP and OpenStep
From NeXTSTEP to OS X to iOS
OSX, Unix, and Cocoa
Introducing the Swift Language
The Cocoa Frameworks
Tools for Cocoa Programming
Some Advice on Learning
1. Let’s Get Started
Creating an Xcode Project
Getting around in Xcode
Application Design
Model-View-Controller
The view layer
The model layer
The controller layer
Creating the MainWindowController class
Creating the User Interface in Interface Builder
Adding view objects
Configuring view objects
XIB files and NIB files
Showing the Window
Making Connections
Creating an outlet
Connecting an outlet
Defining an action method
Connecting actions
Creating the Model Layer
Connecting the Model Layer to the Controller
Improving Controller Design
2. Swift Types
Introducing Swift
Types in Swift
Using Standard Types
Inferring types
Specifying types
Number and boolean types
Collection types
Literals and subscripting
Initializers
Properties
Instance methods
Optionals
Subscripting dictionaries
Loops and String Interpolation
Enumerations and the Switch Statement
Enumerations and raw values
Exploring Apple’s Swift Documentation
3. Structures and Classes
Structures
Instance methods
Using self in instance methods
Operator Overloading
Classes
Designated and convenience initializers
Add an instance method
Inheritance
Computed Properties
Reference and Value Types
Implications of reference and value types
Choosing between reference and value types
Making Types Printable
Swift and Objective-C
Working with Foundation Types
Basic bridging
Bridging with collections
Runtime Errors
More Exploring of Apple’s Swift Documentation
Challenge: Safe Landing
Challenge: Vector Angle
4. Memory Management
Automatic Reference Counting
Objects have reference counts
Deallocating objects in a hierarchy
Strong and Weak References
Strong reference cycles
Unowned references
What is ARC?
5. Controls
Setting up RGBWell
Creating the MainWindowController class
Creating an empty XIB file
Creating an instance of MainWindowController
Connecting a window controller and its window
File's Owner and making connections
About Controls
Working with Controls
A word about NSCell
Connecting the slider’s target and action
A continuous control
Setting the slider’s range values
Adding two more sliders
NSColorWell and NSColor
Disabling a control
Using the Documentation
Changing the color of the color well
Controls and Outlets
Implicitly unwrapped optionals
For the More Curious: More on NSColor
For the More Curious: Setting the Target Programmatically
Challenge: Busy Board
Debugging Hints
6. Delegation
Setting up SpeakLine
Creating and using an Xcode snippet
Creating the user interface
Synthesizing Speech
Updating Buttons
Delegation
Being a delegate
Conforming to a protocol
Implementing a delegate method
Setting the delegate property
Implementing another delegate
Common errors in implementing a delegate
Cocoa classes that have delegates
Delegate protocols and notifications
NSApplication and NSApplicationDelegate
The main event loop
For the More Curious: How Optional Delegate Methods Work
Challenge: Enforcing a Window’s Aspect Ratio
7. Working with Table Views
About Table Views
Delegates and data sources
The table view-data source conversation
SpeakLine’s table view and helper objects
Getting Voice Data
Retrieving friendly names
Adding a Table View
Table view and related objects
Table columns
Disabling resizing of the window
Tables, Cells, and Views
Table cell views
The NSTableViewDataSource Protocol
Conforming to the protocol
Connecting the dataSource outlet
Implementing data source methods
Binding the text field to the table cell view
The NSTableViewDelegate Protocol
Making a connection with the assistant editor
Implementing a delegate method
Pre-selecting the default voice
Challenge: Make a Data Source
8. KVC, KVO, and Bindings
Bindings
Setting up Thermostat
Using bindings
Key-value observing
Making keys observable
Binding other attributes
KVC and Property Accessors
KVC and nil
Debugging Bindings
Using the Debugger
Using breakpoints
Stepping through code
Deleting breakpoints
Setting an exception breakpoint
The LLDB console
Using the debugger to see bindings in action
For the More Curious: Key Paths
For the More Curious: More on Key-Value Observing
For the More Curious: Dependent Keys
Challenge: Convert RGBWell to Use Bindings
9. NSArrayController
RaiseMan’s Model Layer
RaiseMan’s View Layer
Introducing NSArrayController
Adding an Array Controller to the XIB
Binding the Array Controller to the Model
Binding the Table View’s Content to the Array Controller
Connecting the Add Employee Button
Binding the Text Fields to the Table Cell Views
Formatting the Raise Text Field
Connecting the Remove Button
Binding the Table View’s Selection to the Array Controller
Configuring RaiseMan’s Remove Button
Sorting in RaiseMan
How Sorting Works in RaiseMan
For the More Curious: The caseInsensitiveCompare(_:) Method
For the More Curious: Sorting Without NSArrayController
For the More Curious: Filtering
For the More Curious: Using Interface Builder’s View Hierarchy Popover
Challenge: Sorting Names by Length
10. Formatters and Validation
Formatters
Formatters, programmatically
Formatters and a control’s objectValue
Formatters and localization
Validation with Key-Value Coding
Adding Key-Value validation to RaiseMan
For the More Curious: NSValueTransformer
11. NSUndoManager
Message Passing and NSInvocation
How the NSUndoManager Works
Using NSUndoManager
Key-Value Coding and To-Many Relationships
Adding Undo to RaiseMan
Key-Value Observing
Using the Context Pointer Defensively
Undo for Edits
Begin Editing on Insert
For the More Curious: Windows and the Undo Manager
12. Archiving
NSCoder and NSCoding
Encoding
Decoding
The Document Architecture
Info.plist and NSDocumentController
NSDocument
Saving documents
Understanding NSErrorPointer
Loading documents
NSWindowController
Saving and NSKeyedArchiver
Loading and NSKeyedUnarchiver
Setting the Extension and Icon for the File Type
Application Data and URLs
For the More Curious: Preventing Infinite Loops
For the More Curious: Creating a Protocol
For the More Curious: Automatic Document Saving
For the More Curious: Document-Based Applications Without Undo
For the More Curious: Universal Type Identifiers
13. Basic Core Data
Defining the Object Model
Configure the Array Controller
Add the Views
Connections and Bindings
How Core Data Works
Fetching Objects from the NSManagedObjectContext
Persistent Store Types
Choosing a Cocoa Persistence Technology
Customizing Objects Created by NSArrayController
Challenge: Begin Editing on Add
Challenge: Implement RaiseMan Using Core Data
14. User Defaults
NSUserDefaults
Adding User Defaults to SpeakLine
Create Names for the Defaults
Register Factory Defaults for the Preferences
Reading the Preferences
Reflecting the Preferences in the UI
Writing the Preferences to User Defaults
Storing the User Defaults
What Can Be Stored in NSUserDefaults?
Precedence of Types of Defaults
What is the User’s Defaults Database?
For the More Curious: Reading/Writing Defaults from the Command Line
For the More Curious: NSUserDefaultsController
Challenge: Reset Preferences
15. Alerts and Closures
NSAlert
Modals and Sheets
Completion Handlers and Closures
Closures and capturing
Make the User Confirm the Deletion
For the More Curious: Functional Methods and Minimizing Closure Syntax
Challenge: Don’t Fire Them Quite Yet
Challenge: Different Messages for Different Situations
16. Using Notifications
What Notifications Are
What Notifications Are Not
NSNotification
NSNotificationCenter
Starting the Chatter Application
Using Notifications in Chatter
For the More Curious: Delegates and Notifications
Challenge: Beep-beep!
Challenge: Add Usernames
Challenge: Colored Text
Challenge: Disabling the Send Button
17. NSView and Drawing
Setting Up the Dice Application
Creating a view subclass
Views, Rectangles, and Coordinate Systems
frame
bounds
Custom Drawing
drawRect(_:)
When is my view drawn?
Graphics contexts and states
Drawing a die face
Drawing the die profile
Drawing the dots
Dot positioning and drawing
Saving and Restoring the Graphics State
Cleaning up with Auto Layout
Drawing Images
Inspectable properties and designable views
Drawing images with finer control
Scroll Views
Creating Views Programmatically
For the More Curious: Core Graphics and Quartz
For the More Curious: Dirty Rects
For the More Curious: Flipped Views
Challenge: Gradients
Challenge: Stroke
Challenge: Make DieView Configurable from Interface Builder
18. Mouse Events
NSResponder
NSEvent
Getting Mouse Events
Click to Roll
Improving Hit Detection
Gesture Recognizers
Challenge: NSBezierPath-based Hit Testing
Challenge: A Drawing App
19. Keyboard Events
NSResponder
NSEvent
Adding Keyboard Input to DieView
Accept first responder
Receive keyboard events
Putting the dice in Dice
Focus Rings
The Key View Loop
For the More Curious: Rollovers
20. Drawing Text with Attributes
NSFont
NSAttributedString
Drawing Strings and Attributed Strings
Drawing Text Die Faces
Extensions
Getting Your View to Generate PDF Data
For the More Curious: NSFontManager
Challenge: Color Text as SpeakLine Speaks It
21. Pasteboards and Nil-Targeted Actions
NSPasteboard
Add Cut, Copy, and Paste to Dice
Nil-Targeted Actions
Looking at the XIB file
Menu Item Validation
For the More Curious: Which Object Sends the Action Message?
For the More Curious: UTIs and the Pasteboard
Custom UTIs
For the More Curious: Lazy Copying
Challenge: Write Multiple Representations
Challenge: Menu Item
22. Drag-and-Drop
Make DieView a Drag Source
Starting a drag
After the drop
Make DieView a Drag Destination
registerForDraggedTypes(_:)
Add highlighting
Implement the dragging destination methods
For the More Curious: Operation Mask
23. NSTimer
NSTimer-based Animation
How Timers Work
NSTimer and Strong/Weak References
For the More Curious: NSRunLoop
24. Sheets
Adding a Sheet
Create the Window Controller
Set Up the Menu Item
Lay Out the Interface
Configuring the Die Views
Present the Sheet
Modal Windows
Encapsulating Presentation APIs
Challenge: Encapsulate Sheet Presentation
Challenge: Add Menu Item Validation
25. Auto Layout
What is Auto Layout?
Adding Constraints to RaiseMan
Constraints from subview to superview
Constraints between siblings
Size constraints
Intrinsic Content Size
Creating Layout Constraints Programmatically
Visual Format Language
Does Not Compute, Part 1: Unsatisfiable Constraints
Does Not Compute, Part 2: Ambiguous Layout
For the More Curious: Autoresizing Masks
Challenge: Add Vertical Constraints
Challenge: Add Constraints Programmatically
26. Localization and Bundles
Different Mechanisms for Localization
Localizing a XIB File
Localizing String Literals
Demystifying NSLocalizedString and genstrings
Explicit Ordering of Tokens in Format Strings
NSBundle
NSBundle’s role in localization
Loading code from bundles
For the More Curious: Localization and Plurality
Challenge: Localizing the Default Name for a Newly Added Employee
Challenge: Localizing the Undo Action Names
27. Printing
Dealing with Pagination
Adding Printing to RaiseMan
For the More Curious: Are You Drawing to the Screen?
Challenge: Add Page Numbers
Challenge: Persist Page Setup
28. Web Services
Web Services APIs
RanchForecast Project
NSURLSession and asynchronous API design
NSURLSession, HTTP status codes, and errors
Add JSON parsing to ScheduleFetcher
Lay out the interface
Opening URLs
Safely Working with Untyped Data Structures
For the More Curious: Parsing XML
Challenge: Improve Error Handling
Challenge: Add a Spinner
Challenge: Parse the XML Courses Feed
29. Unit Testing
Testing in Xcode
Your First Test
A Note on Literals in Testing
Creating a Consistent Testing Environment
Sharing Constants
Refactoring for Testing
For the More Curious: Access Modifiers
For the More Curious: Asynchronous Testing
Challenge: Make Course Implement Equatable
Challenge: Improve Test Coverage of Web Service Responses
Challenge: Test Invalid JSON Dictionary
30. View Controllers
NSViewController
Starting the ViewControl Application
Windows, Controllers, and Memory Management
Container View Controllers
Add a Tab View Controller
View Controllers vs. Window Controllers
Considerations for OS X 10.9 and Earlier
Challenge: SpeakLineViewController
Challenge: Programmatic View Controller
Challenge: Add a Window Controller
31. View Swapping and Custom Container View Controllers
View Swapping
NerdTabViewController
Adding Tab Images
Challenge: Boxless NerdTabViewController
Challenge: NerdSplitViewController
Challenge: Draggable Divider
32. Storyboards
A New UI for RanchForecast
Adding the course list
Adding the web view
Connecting the Course List Selection with the Web View
Creating the CourseListViewControllerDelegate
Creating the parent view controller
For the More Curious: How is the Storyboard Loaded?
33. Core Animation
CALayer
Scattered
Implicit Animation and Actions
More on CALayer
Challenge: Show Filenames
Challenge: Reposition Image Layers
34. Concurrency
Multithreading
A Deep Chasm Opens Before You
Improving Scattered: Time Profiling in Instruments
Introducing Instruments
Analyzing output from Instruments
NSOperationQueue
Multithreaded Scattered
Thread synchronization
For the More Curious: Faster Scattered
Challenge: An Even Better Scattered
35. NSTask
ZIPspector
Asynchronous Reads
iPing
Challenge: .tar and .tgz Files
36. Distributing Your App
Build Configurations
Preprocessor Directives: Using Build Configurations to Change Behavior
Creating a Release Build
A Few Words on Installers
App Sandbox
Entitlements
Containers
Mediated file access and Powerbox
The Mac App Store
Receipt Validation
Local receipt verification
Server-based verification
37. Afterword
Index
← Prev
Back
Next →
← Prev
Back
Next →