logo资料库

Swift Development with Cocoa.pdf

第1页 / 共472页
第2页 / 共472页
第3页 / 共472页
第4页 / 共472页
第5页 / 共472页
第6页 / 共472页
第7页 / 共472页
第8页 / 共472页
资料共472页,剩余部分请下载后查看
Copyright
Table of Contents
Preface
Objective-C to Swift
Audience
Organization of This Book
Conventions Used in This Book
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgments
Chapter 1. Cocoa Development Tools
The Mac and iOS Developer Programs
Registering for a Developer Program
Downloading Xcode
Creating Your First Project with Xcode
The Xcode Interface
Developing a Simple Swift Application
Designing the Interface
Connecting the Code
Using the iOS Simulator
Testing iOS Apps with TestFlight
Chapter 2. Programming with Swift
The Swift Programming Language
Playgrounds
Variables and Constants
Types
Tuples
Arrays
Dictionaries
Control Flow
Switches
Functions and Closures
Using Functions as Variables
Closures
Objects
Inheritance
Initialization and Deinitialization
Properties
Protocols
Extensions
Access Control
Operators
Generics
Interoperating with Objective-C
Using Objective-C and Swift in the Same Project
Using Swift Objects in Objective-C
Using Objective-C Objects in Swift
Modules
Memory Management
Working with Strings
Comparing Strings
Searching Strings
Data
Loading Data from Files and URLs
Serialization and Deserialization
Design Patterns in Cocoa
Model-View-Controller
Delegation
Chapter 3. Applications on OS X and iOS
What Is an Application?
Applications, Frameworks, Utilities, and More
What Are Apps Composed Of?
Using NSBundle to Find Resources in Applications
The Application Life Cycle
OS X Applications
iOS Applications
The Application Sandbox
Application Restrictions
Notifications with NSNotification
Chapter 4. Graphical User Interfaces
Interfaces in OS X and iOS
MVC and Application Design
Nib Files and Storyboards
Structure of a Nib File
Storyboards
Outlets and Actions
How Nib Files and Storyboards Are Loaded
Constructing an Interface
Guidelines and Constraints
Building an App with Nibs and Constraints
Interfaces on iOS
Launch Screen Files
UI Dynamics
UI and Gravity
Snapping UI
Core Animation
Layers
Animations
Chapter 5. Closures and Operation Queues
Closures in Cocoa
Concurrency with Operation Queues
Operation Queues and NSOperation
Performing Work on Operation Queues
Putting It All Together
Chapter 6. Drawing Graphics in Views
How Drawing Works
The Pixel Grid
Retina Displays
Pixels and Screen Points
Drawing in Views
Frame Rectangles
Bounds Rectangles
Building a Custom View
Filling with a Solid Color
Working with Paths
Creating Custom Paths
Multiple Subpaths
Shadows
Gradients
Transforms
Chapter 7. SpriteKit
SpriteKit’s Architecture
Making an App That Uses SpriteKit
Working with SpriteKit Scenes
SpriteKit Nodes
Putting Sprites in Scenes
Responding to Touches
Working with Textures
Texture Atlases
Working with Text
Animating Content with Actions
Using Shape Nodes
Using Image Effect Nodes
Adding Physics to SpriteKit Objects
Adding Joints to SpriteKit Objects
Lighting SpriteKit Scenes
Constraints
Using Shaders in SpriteKit
Using SpriteKit Editor
Chapter 8. SceneKit
SceneKit Structure
Working with SceneKit
Adding a SceneKit View
Adding a Scene
Adding a Camera
Adding a 3D Object
Adding Lights
Animating Content in the Scene
Creating Text Geometry
Combining Animations
Working with Materials
Normal Mapping
Hit Testing
Constraints
Loading Data from COLLADA Files
Adding Physics to the Scene
Chapter 9. Audio and Video
AV Foundation
Playing Video with AVPlayer
AVPlayerLayer
Putting It Together
AVKit
AVKit on iOS
Playing Sound with AVAudioPlayer
Speech Synthesis
Working with the Photo Library
Capturing Photos and Video from the Camera
Building a Photo Application
The Photo Library
Chapter 10. iCloud and Data Storage
Preferences
Registering Default Preferences
Accessing Preferences
Setting Preferences
Working with the Filesystem
Using NSFileManager
File Storage Locations
Working with the Sandbox
Enabling Sandboxing
Open and Save Panels
Security-Scoped Bookmarks
iCloud
What iCloud Stores
Setting Up for iCloud
Testing Whether iCloud Works
Storing Settings
Handling External Changes
The iOS Counterpart
iCloud Storage
iCloud Storage on OS X
iCloud Storage on iOS
Document Pickers
Using iCloud Well
Chapter 11. Cocoa Bindings
Binding Views to Models
A Simple Bindings App
Binding to Controllers
Array and Object Controllers
A More Complex Bindings App
Chapter 12. Table Views and Collection Views
Data Sources and Delegates
Table Views
UITableView on iOS
NSTableView on OS X
Collection Views
UICollectionView on iOS
Chapter 13. Document-Based Applications
The NSDocument and UIDocument Classes
Document Objects in MVC
Kinds of Documents
The Role of Documents
Document-Based Applications on OS X
Autosaving and Versions
Representing Documents with NSDocument
Saving Simple Data
Saving More Complex Data
Document-Based Applications on iOS
Chapter 14. Networking
Connections
NSURL
NSURLRequest
NSURLSession
NSURLResponse and NSHTTPURLResponse
Building a Networked Application
Bonjour Service Discovery
Browsing for Shared iTunes Libraries
Multipeer Connectivity
Chapter 15. Working with the Real World
Working with Location
Location Hardware
The Core Location Framework
Working with Core Location
Geocoding
Region Monitoring and iBeacons
Locations and Privacy
Maps
Using Maps
Annotating Maps
Maps and Overlays
Device Motion
Working with Core Motion
Using the Built-in Altimeter
Using the Pedometer
Printing Documents
Printing on OS X
Printing on iOS
Game Controllers
App Nap
Authenticating Using Touch ID
Handoff
Chapter 16. EventKit
Understanding Events
Accessing the Event Store
Accessing Calendars
Accessing Events
Working with Events
Building an Events Application
User Privacy
Chapter 17. Instruments and the Debugger
Getting Started with Instruments
The Instruments Interface
Observing Data
Adding Instruments from the Library
Fixing Problems Using Instruments
Retain Cycles and Leaks
Using the Debugger
Setting Breakpoints
Inspecting Memory Contents
Working with the Debugger Console
View Debugging
The Testing Framework
Writing Tests
Writing Asynchronous Tests
Performance-Testing Blocks with Tests
Debug Gauges
Performance Optimization
Chapter 18. Sharing and Notifications
Sharing
Sharing on iOS
Sharing on OS X
Notifications
Registering Notification Settings
Push Notifications
What Happens When a Notification Arrives
Sending Push Notifications
Setting Up to Receive Push Notifications
Receiving Push Notifications
Local Notifications
Chapter 19. Nonstandard Apps
Command-Line Tools
Preference Panes
How Preference Panes Work
Preference Domains
Building a Sample Preference Pane
Status Bar Items
Building a Status Bar App
iOS Apps with Multiple Windows
Chapter 20. Working with Text
Internationalization and Localization
Strings Files
Creating a Sample Localized Application
Formatting Data with NSFormatter
Testing Different Locales
Formatting Numbers, Lengths, Mass, Energy, and Data
NSNumberFormatter
NSEnergyFormatter, NSMassFormatter, and NSLengthFormatter
NSByteCountFormatter
Detecting Data with NSDataDetector
TextKit
Index
About the Authors
Swift Development with Cocoa Ready to build apps for iPhone, iPad, and Mac now that Swift has landed? If you’re an experienced programmer who’s never touched Apple developer tools, this hands-on book shows you how to use the Swift language to make incredible iOS and OS X apps, using Cocoa and Cocoa Touch. Learn how to use Swift in a wide range of real-world situations, with Cocoa features such as EventKit and Core Animation. You’ll pick up Swift language features and syntax along the way, and understand why using Swift (instead of Objective-C) makes iOS and Mac app development easier, faster, and safer. You’ll also work with several exercises to help you practice as you learn. ■ Learn the OS X and iOS application lifecycle ■ Use storyboards to design adaptive interfaces ■ Explore graphics systems, including the built-in 2D and 3D game frameworks ■ Display video and audio with AVFoundation ■ Store data locally with the filesystem, or on the network with iCloud ■ Display lists or collections of data with table views and collection views ■ Build apps that let users create, edit, and work with documents ■ Use MapKit, Core Location, and Core Motion to interact with the world Jonathan Manning, cofounder of Secret Lab, is a mobile software engineer, game designer, and computing researcher. Follow him on Twitter at @desplesda. Paris Buttfield-Addison, also a cofounder of Secret Lab, is a mobile software engineer, game designer, and computing researcher. Follow him on Twitter at @parisba. Tim Nugent, not a cofounder of Secret Lab, is a mobile app developer, game designer, PhD student, and author. Follow him on Twitter at @The_McJones. MACINTOSH/IPAD & IPHONE US $39.99 CAN $41.99 ISBN: 978-1-491-90894-5 Twitter: @oreillymedia facebook.com/oreilly w i t h C o c o a S w l i f t D e v e o p m e n t l B u t t fi e d - A d d i s o n M a n n n g , i & N u g e n t Swift Development with Cocoa DEVELOPING FOR THE MAC AND IOS APP STORES Jonathon Manning, Paris Buttfield-Addison & Tim Nugent www.it-ebooks.info
Swift Development with Cocoa Ready to build apps for iPhone, iPad, and Mac now that Swift has landed? If you’re an experienced programmer who’s never touched Apple developer tools, this hands-on book shows you how to use the Swift language to make incredible iOS and OS X apps, using Cocoa and Cocoa Touch. Learn how to use Swift in a wide range of real-world situations, with Cocoa features such as EventKit and Core Animation. You’ll pick up Swift language features and syntax along the way, and understand why using Swift (instead of Objective-C) makes iOS and Mac app development easier, faster, and safer. You’ll also work with several exercises to help you practice as you learn. ■ Learn the OS X and iOS application lifecycle ■ Use storyboards to design adaptive interfaces ■ Explore graphics systems, including the built-in 2D and 3D game frameworks ■ Display video and audio with AVFoundation ■ Store data locally with the filesystem, or on the network with iCloud ■ Display lists or collections of data with table views and collection views ■ Build apps that let users create, edit, and work with documents ■ Use MapKit, Core Location, and Core Motion to interact with the world Jonathon Manning, cofounder of Secret Lab, is a mobile software engineer, game designer, and computing researcher. Follow him on Twitter at @desplesda. Paris Buttfield-Addison, also a cofounder of Secret Lab, is a mobile software engineer, game designer, and computing researcher. Follow him on Twitter at @parisba. Tim Nugent, not a cofounder of Secret Lab, is a mobile app developer, game designer, PhD student, and author. Follow him on Twitter at @The_McJones. MACINTOSH/IPAD & IPHONE US $39.99 CAN $41.99 ISBN: 978-1-491-90894-5 Twitter: @oreillymedia facebook.com/oreilly www.it-ebooks.info w i t h C o c o a S w l i f t D e v e o p m e n t l B u t t fi e d - A d d i s o n M a n n n g , i & N u g e n t Swift Development with Cocoa DEVELOPING FOR THE MAC AND IOS APP STORES Jonathon Manning, Paris Buttfield-Addison & Tim Nugent
Swift Development with Cocoa Jonathon Manning, Paris Buttfield-Addison, and Tim Nugent www.it-ebooks.info
Swift Development with Cocoa by Jonathon Manning, Paris Buttfield-Addison, and Tim Nugent Copyright © 2015 Secret Lab. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com. Editor: Rachel Roumeliotis Production Editor: Matthew Hacker Copyeditor: Jasmine Kwityn Proofreader: Charles Roumeliotis Indexer: Wendy Catalano Cover Designer: Ellie Volckhausen Interior Designer: David Futato Illustrator: Rebecca Demarest December 2014: First Edition Revision History for the First Edition: 2014-12-08: First release See http://oreilly.com/catalog/errata.csp?isbn=9781491908945 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Swift Development with Cocoa, the cover image of an Australasian gannet, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While the publisher and the authors have used good faith efforts to ensure that the information and in‐ structions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. ISBN: 978-1-491-90894-5 [M] www.it-ebooks.info
Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi 1. Cocoa Development Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 The Mac and iOS Developer Programs 1 Registering for a Developer Program 2 Downloading Xcode 3 Creating Your First Project with Xcode 4 The Xcode Interface 7 Developing a Simple Swift Application 13 Designing the Interface 13 Connecting the Code 15 Using the iOS Simulator 17 Testing iOS Apps with TestFlight 19 2. Programming with Swift. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 The Swift Programming Language 21 Playgrounds 23 Variables and Constants 24 Types 26 Tuples 28 Arrays 28 Dictionaries 29 Control Flow 30 Switches 33 Functions and Closures 35 Using Functions as Variables 37 Closures 39 Objects 40 Inheritance 42 www.it-ebooks.info iii
Initialization and Deinitialization 42 Properties 44 Protocols 46 Extensions 47 Access Control 49 Operators 50 Generics 51 Interoperating with Objective-C 52 Using Objective-C and Swift in the Same Project 52 Using Swift Objects in Objective-C 53 Using Objective-C Objects in Swift 53 Modules 54 Memory Management 54 Working with Strings 55 Comparing Strings 56 Searching Strings 57 Data 57 Loading Data from Files and URLs 57 Serialization and Deserialization 58 Design Patterns in Cocoa 59 Model-View-Controller 59 Delegation 60 3. Applications on OS X and iOS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 What Is an Application? 63 Applications, Frameworks, Utilities, and More 64 What Are Apps Composed Of? 65 Using NSBundle to Find Resources in Applications 67 The Application Life Cycle 68 OS X Applications 68 iOS Applications 70 The Application Sandbox 75 Application Restrictions 76 Notifications with NSNotification 78 4. Graphical User Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Interfaces in OS X and iOS 81 MVC and Application Design 82 Nib Files and Storyboards 82 Structure of a Nib File 83 Storyboards 86 Outlets and Actions 87 iv | Table of Contents www.it-ebooks.info
How Nib Files and Storyboards Are Loaded 88 Constructing an Interface 89 Guidelines and Constraints 90 Building an App with Nibs and Constraints 91 Interfaces on iOS 95 Launch Screen Files 97 UI Dynamics 98 UI and Gravity 98 Snapping UI 99 Core Animation 100 Layers 101 Animations 102 5. Closures and Operation Queues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Closures in Cocoa 106 Concurrency with Operation Queues 107 Operation Queues and NSOperation 108 Performing Work on Operation Queues 108 Putting It All Together 109 6. Drawing Graphics in Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 How Drawing Works 115 The Pixel Grid 117 Retina Displays 118 Pixels and Screen Points 119 Drawing in Views 120 Frame Rectangles 120 Bounds Rectangles 121 Building a Custom View 122 Filling with a Solid Color 123 Working with Paths 124 Creating Custom Paths 126 Multiple Subpaths 128 Shadows 129 Gradients 132 Transforms 135 7. SpriteKit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 SpriteKit’s Architecture 139 Making an App That Uses SpriteKit 140 Working with SpriteKit Scenes 141 SpriteKit Nodes 143 Table of Contents | v www.it-ebooks.info
Putting Sprites in Scenes 145 Responding to Touches 146 Working with Textures 147 Texture Atlases 148 Working with Text 149 Animating Content with Actions 150 Using Shape Nodes 151 Using Image Effect Nodes 153 Adding Physics to SpriteKit Objects 154 Adding Joints to SpriteKit Objects 155 Lighting SpriteKit Scenes 156 Constraints 157 Using Shaders in SpriteKit 157 Using SpriteKit Editor 160 8. SceneKit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 SceneKit Structure 164 Working with SceneKit 165 Adding a SceneKit View 165 Adding a Scene 166 Adding a Camera 167 Adding a 3D Object 168 Adding Lights 169 Animating Content in the Scene 170 Creating Text Geometry 172 Combining Animations 173 Working with Materials 174 Normal Mapping 176 Hit Testing 178 Constraints 181 Loading Data from COLLADA Files 182 Adding Physics to the Scene 185 9. Audio and Video. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 AV Foundation 189 Playing Video with AVPlayer 190 AVPlayerLayer 191 Putting It Together 191 AVKit 195 AVKit on iOS 197 Playing Sound with AVAudioPlayer 199 Speech Synthesis 200 vi | Table of Contents www.it-ebooks.info
分享到:
收藏