Download from Wow! eBook SECOND EDITION Programming iOS Matt Neuburg Beijing • Cambridge • Farnham • Kưln • Sebastopol • Tokyo Programming iOS 5, Second Edition by Matt Neuburg Copyright © 2012 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: Brian Jepson Production Editor: Kristen Borg Proofreader: O’Reilly Production Services March 2012: Indexer: Matt Neuburg Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Matt Neuburg Second Edition Revision History for the Second Edition: 2011-12-23 Early release 2012-03-12 First release See http://oreilly.com/catalog/errata.csp?isbn=9781449319342 for release details Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc Programming iOS 5, the image of a kingbird, 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 every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein ISBN: 978-1-449-31934-2 [M] 1331571373 Table of Contents Preface xvii Part I Language Just Enough C Compilation, Statements, and Comments Variable Declaration, Initialization, and Data Types Structs Pointers Arrays Operators Flow Control and Conditions Functions Pointer Parameters and the Address Operator Files The Standard Library More Preprocessor Directives Data Type Qualifiers 10 13 14 16 20 23 25 27 28 29 Object-Based Programming 31 Objects Messages and Methods Classes and Instances Class Methods Instance Variables The Object-Based Philosophy 31 32 33 36 37 38 Objective-C Objects and Messages 43 An Instance Reference Is a Pointer Instance References, Initialization, and nil 43 44 iii About the Author Matt Neuburg has a PhD in Classics and has taught at many universities and colleges He has been programming computers since 1968 He has written applications for Mac OS X and iOS, is a former editor of MacTech Magazine, and is a long-standing contributing editor for TidBITS His previous O'Reilly books are Frontier: The Definitive Guide, REALbasic: The Definitive Guide, and AppleScript: The Definitive Guide He makes a living writing books, articles, and software documentation, as well as by programming, consulting, and training Colophon The animal on the cover of Programming iOS is a kingbird, one of the 13 species of North American songbirds making up the genus Tyrannus A group of kingbirds is called a “coronation,” a “court,” or a “tyranny.” Kingbirds eat insects, which they often catch in flight, swooping from a perch to grab the insect midair They may also supplement their diets with berries and fruits They have long, pointed wings, and males perform elaborate aerial courtship displays Both the genus name (meaning “tyrant” or “despot”) and the common name (“kingbird”) refer to these birds’ aggressive defense of their territories, breeding areas, and mates They have been documented attacking red-tailed hawks (which are more than twenty times their size), knocking bluejays out of trees, and driving away crows and ravens (For its habit of standing up to much larger birds, the gray kingbird has been adopted as a Puerto Rican nationalist symbol.) “Kingbird” most often refers to the Eastern kingbird (T tyrannus), an average-size kingbird (7.5–9 inches long, wingspan 13–15 inches) found all across North America This common and widespread bird has a dark head and back, with a white throat, chest, and belly Its red crown patch is rarely seen Its high-pitched, buzzing, stuttering sounds have been described as resembling “sparks jumping between wires” or an electric fence The cover image is from Cassell’s Natural History The cover font is Adobe ITC Garamond The text font is Linotype Birka; the heading font is Adobe Myriad Condensed; and the code font is LucasFont’s TheSansMonoCondensed Download from Wow! eBook ... SECOND EDITION Programming iOS Matt Neuburg Beijing • Cambridge • Farnham • Kưln • Sebastopol • Tokyo Programming iOS 5, Second Edition by Matt Neuburg Copyright © 2012 Matt Neuburg... Futato Illustrator: Matt Neuburg Second Edition Revision History for the Second Edition: 2011 -12-23 Early release 2012 -03-12 First release See http:/ /oreilly. com/catalog/errata.csp?isbn=9781449319342... and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc Programming iOS 5, the image of a kingbird, and related trade dress are trademarks of O’Reilly Media, Inc Many of the designations