www.it-ebooks.info www.it-ebooks.info Matt Neuburg iOS 7 Programming Fundamentals Objective-C, Xcode, and Cocoa Basics www.it-ebooks.info iOS 7 Programming Fundamentals by Matt Neuburg Copyright © 2014 Matt Neuburg. 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://my.safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com. Editor: Rachel Roumeliotis Production Editor: Kristen Brown Proofreader: O’Reilly Production Services Indexer: Matt Neuburg Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Matt Neuburg October 2013: First Edition Revision History for the First Edition: 2013-10-10: First release See http://oreilly.com/catalog/errata.csp?isbn=9781491945575 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. iOS 7 Programming Fundamentals, the image of a harp seal, and related trade dress are trade‐ marks 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 trade‐ mark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-1-491-94557-5 [LSI] www.it-ebooks.info Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Part I. Language 1. Just Enough C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Compilation, Statements, and Comments 5 Variable Declaration, Initialization, and Data Types 6 Structs 9 Pointers 11 Arrays 13 Operators 15 Flow Control and Conditions 17 Functions 21 Pointer Parameters and the Address Operator 24 Files 25 The Standard Library 29 More Preprocessor Directives 30 Data Type Qualifiers 31 2. Object-Based Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Objects 33 Messages and Methods 34 Classes and Instances 35 Class Methods 37 Instance Variables 38 iii www.it-ebooks.info The Object-Based Philosophy 40 3. Objective-C Objects and Messages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 An Object Reference Is a Pointer 45 Instance References, Initialization, and nil 47 Instance References and Assignment 50 Instance References and Memory Management 51 Methods and Messages 52 Calling a Method 53 Declaring a Method 54 Nesting Method Calls 55 No Overloading 56 Parameter Lists 57 When Message Sending Goes Wrong 58 Messages to nil 59 Unrecognized Selectors 60 Typecasting and the id Type 61 Messages as Data Type 65 C Functions 67 CFTypeRefs 68 Blocks 69 4. Objective-C Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Subclass and Superclass 75 Interface and Implementation 77 Header File and Implementation File 79 Class Methods 81 The Secret Life of Classes 82 5. Objective-C Instances. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 How Instances Are Created 85 Ready-Made Instances 85 Instantiation from Scratch 86 Nib-Based Instantiation 90 Polymorphism 91 The Keyword self 93 The Keyword super 97 Instance Variables and Accessors 98 Key–Value Coding 101 Properties 103 iv | Table of Contents www.it-ebooks.info How to Write an Initializer 104 Referring to Instances 108 Part II. IDE 6. Anatomy of an Xcode Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 New Project 114 The Project Window 116 The Navigator Pane 117 The Utilities Pane 123 The Editor 124 The Project File and Its Dependents 127 The Target 130 Build Phases 131 Build Settings 133 Configurations 134 Schemes and Destinations 135 Renaming Parts of a Project 138 From Project to Running App 139 Build Settings 141 Property List Settings 142 Nib Files 143 Additional Resources 144 Code and the App Launch Process 146 Frameworks and SDKs 150 7. Nib Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 A Tour of the Nib Editor Interface 156 The Document Outline 158 Canvas 161 Inspectors and Libraries 163 Nib Loading 164 Outlets and the Nib Owner 167 Creating an Outlet 172 Misconfiguring an Outlet 174 Deleting an Outlet 175 More Ways to Create Outlets 176 Outlet Collections 180 Action Connections 180 Table of Contents | v www.it-ebooks.info Additional Initialization of Nib-Based Instances 183 8. Documentation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 The Documentation Window 188 Class Documentation Pages 190 Sample Code 193 Other Resources 194 Quick Help 194 Symbols 195 Header Files 196 Internet Resources 197 9. Life Cycle of a Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Device Architecture and Conditional Code 199 Version Control 203 Editing Your Code 206 Autocompletion 207 Snippets 209 Fix-it and Live Syntax Checking 209 Navigating Your Code 210 Running in the Simulator 213 Debugging 214 Caveman Debugging 214 The Xcode Debugger 217 Unit Testing 223 Static Analyzer 227 Clean 229 Running on a Device 230 Obtaining a Certificate 233 Obtaining a Development Provisioning Profile 235 Running the App 237 Profile and Device Management 237 Gauges and Instruments 238 Localization 244 Archiving and Distribution 249 Ad Hoc Distribution 251 Final App Preparations 252 Icons in the App 253 Other Icons 255 Launch Images 256 vi | Table of Contents www.it-ebooks.info Screenshots 257 Property List Settings 258 Submission to the App Store 260 Part III. Cocoa 10. Cocoa Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Subclassing 265 Categories 268 Splitting a Class 270 Class Extensions 271 Protocols 272 Informal Protocols 276 Optional Methods 277 Some Foundation Classes 278 Useful Structs and Constants 278 NSString and Friends 279 NSDate and Friends 281 NSNumber 282 NSValue 284 NSData 284 Equality and Comparison 284 NSIndexSet 285 NSArray and NSMutableArray 286 NSSet and Friends 288 NSDictionary and NSMutableDictionary 288 NSNull 291 Immutable and Mutable 291 Property Lists 292 The Secret Life of NSObject 293 11. Cocoa Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Reasons for Events 298 Subclassing 298 Notifications 300 Receiving a Notification 301 Unregistering 303 Posting a Notification 304 NSTimer 305 Table of Contents | vii www.it-ebooks.info Delegation 306 Cocoa Delegation 306 Implementing Delegation 308 Data Sources 311 Actions 312 The Responder Chain 315 Deferring Responsibility 316 Nil-Targeted Actions 316 Swamped by Events 317 Delayed Performance 320 12. Accessors and Memory Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 Accessors 323 Key–Value Coding 324 KVC and Outlets 326 Key Paths 327 Array Accessors 328 Memory Management 329 Principles of Cocoa Memory Management 330 The Rules of Cocoa Manual Memory Management 331 What ARC Is and What It Does 334 How Cocoa Objects Manage Memory 336 Autorelease 338 Memory Management of Instance Variables (Non-ARC) 341 Memory Management of Instance Variables (ARC) 345 Retain Cycles and Weak References 346 Unusual Memory Management Situations 349 Nib Loading and Memory Management 354 Memory Management of Global Variables 355 Memory Management of CFTypeRefs 356 Memory Management of Pointer-to-Void Context Info 359 Properties 361 Property Memory Management Policies 362 Property Declaration Syntax 363 Property Accessor Synthesis 365 Dynamic Accessors 368 13. Communication Between Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 Visibility by Instantiation 374 Visibility by Relationship 376 viii | Table of Contents www.it-ebooks.info [...]... underpinnings might in fact be desirable, iOS 7 Programming Fundamentals will still be available and awaiting study As for this book, iOS 7 Programming Fundamentals itself, it is the prodigal child, the book I originally wanted to write but which was then subsumed during three editions into the larger world of Programming iOS 4, Programming iOS 5, and Programming iOS 6 Now it is home again, where it belongs,... Notifications Key–Value Observing Model–View–Controller 377 378 379 385 Index 389 Table of Contents www.it-ebooks.info | ix www.it-ebooks.info Preface After three editions of my book on programming iOS — Programming iOS 4 (May 2011), Programming iOS 5 (March 2012), and Programming iOS 6 (March 2013) — it is as if a friendly but powerful... now two books: • This book, iOS 7 Programming Fundamentals, comprising chapters 1–13 of the earlier books • The other book, Programming iOS 7, comprising chapters 14–40 of the earlier books The giant was friendly, because it was high time Programming iOS 6 had grown to an unwieldy and forbidding 1150 pages In fact, I had been begging for this giant even before Programming iOS 4 was published — indeed,... program for iOS, you need to speak to iOS Everything you say to iOS will be in accordance with the iOS API (An API, for application programming interface, is a list or specification of things you are allowed to say when communicating.) Therefore, you will need some knowledge of the C programming language, for two reasons: • Most of the iOS API involves the Objective-C language, and most of your iOS programming. .. done) complain that the book is occupied with 13 preliminary chapters before the reader starts really writing any significant iOS code, because those 13 chapters have now been abstracted into a separate volume, iOS 7 Programming Fundamentals, and the other book, Programming iOS 7, now begins, like Homer’s Iliad, in the middle of the story, with the reader jumping with all four feet into views and view... proposal to O’Reilly Media, back in early 2010, had been for a book to be called Fundamentals of Cocoa Programming, intended to cover very much the same material as the present book, iOS 7 Programming Fundamentals But the proposal was accepted only on condition that it be extended to cover much more of Cocoa Touch (iOS) programming; so I complied and set to work on this longer project, and later, despite... publisher: O’Reilly Media, Inc 1005 Gravenstein Highway North Sebastopol, CA 95 472 800-998-9938 (in the United States or Canada) 70 7-829-0515 (international or local) 70 7-829-0104 (fax) We have a web page for this book, where we list errata, examples, and any additional information You can access this page at http://oreil.ly /ios7 _programming_ fundamen tals To comment or ask technical questions about this... say, O’Reilly Media were implacably opposed Thus, Programming iOS 7, though it starts with its own Chapter 1 and page 1, never‐ theless still picks up exactly where iOS 7 Programming Fundamentals leaves off They xi www.it-ebooks.info complement and supplement one another Those who desire a complete grounding in the knowledge needed to begin writing iOS apps with a solid and rigorous understand‐ ing... use my own real apps and real programming situations to illustrate and motivate its explanations And then you’ll be ready for Programming iOS 7, of course! Versions This book is geared to iOS 7 and Xcode 5 In general, only very minimal attention is given to earlier versions of iOS and Xcode It is not my intention to embrace in this book any detailed knowledge about earlier versions of the software, which... require permission We appreciate, but do not require, attribution An attribution usually includes the title, author, publisher, and ISBN For example: iOS 7 Programming Fundamentals by Matt Neuburg (O’Reilly) Copyright 2014 Matt Neuburg, 978 -1-491-945 57- 5.” If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com xvi . 268 Splitting a Class 270 Class Extensions 271 Protocols 272 Informal Protocols 276 Optional Methods 277 Some Foundation Classes 278 Useful Structs and Constants 278 NSString and Friends 279 NSDate and. www.it-ebooks.info www.it-ebooks.info Matt Neuburg iOS 7 Programming Fundamentals Objective-C, Xcode, and Cocoa Basics www.it-ebooks.info iOS 7 Programming Fundamentals by Matt Neuburg Copyright © 2014. ix www.it-ebooks.info www.it-ebooks.info Preface After three editions of my book on programming iOS — Programming iOS 4 (May 2011), Programming iOS 5 (March 2012), and Programming iOS 6 (March 2013) — it is as if a friendly but