20,99 €
A step-by-step guide to understanding object-oriented programming with Objective-C
As the primary programming language for iPhone, iPad, and Mac OS X applications, Objective-C is a reflective, object-oriented language that all programmers must know before creating apps. Assuming no prior programming language experience, this fun-and-friendly book provides you with a solid understanding of Objective-C. Addressing the latest version of Xcode, debugging, code completion, and more, veteran author Neal Goldstein helps you gain a solid foundation of this complex topic, and filters out any unnecessary intricate technical jargon.
Objective-C Programming For Dummies is the ideal beginner book if your objective is to venture into iPhone, iPad, and Mac OS X development for the first time!
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 536
Veröffentlichungsjahr: 2012
Visit www.dummies.com/cheatsheet/objectivecprogramming to view this book's cheat sheet.
Table of Contents
Objective-C® Programming For Dummies®
by Neal Goldstein and Karl G. Kowalski
Objective-C® Programming For Dummies®
Published courtesy of EMC Corporation John Wiley & Sons, Inc.111 River St.Hoboken, NJ 07030-5774
www.wiley.com
Copyright © 2013 by John Wiley & Sons, Inc., Hoboken, New Jersey
Published by John Wiley & Sons, Inc., Hoboken, New Jersey
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.
Trademarks: Wiley, the Wiley logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates in the United States and other countries, and may not be used without written permission. Objective-C is a registered trademark of Apple, Inc. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Website is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Website may provide or recommendations it may make. Further, readers should be aware that Internet Websites listed in this work may have changed or disappeared between when this work was written and when it is read.
For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.
For technical support, please visit www.wiley.com/techsupport.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number: 2012949144
ISBN 978-1-118-21398-8 (pbk); ISBN 978-1-118-22878-4 (ebk); ISBN 978-1-118-26609-0 (ebk); ISBN 978-1-118-23128-9 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
About the Authors
Neal Goldstein is a recognized leader in making state-of-the-art and cutting-edge technologies practical for commercial and enterprise development. He was one of the first technologists to work with commercial developers at firms such as Apple Computer, Lucasfilm, and Microsoft to develop commercial applications using object-based programming technologies. He was a pioneer in moving that approach into the corporate world for developers at Liberty Mutual Insurance, USWest (now Verizon), National Car Rental, EDS, and Continental Airlines, showing them how object-oriented programming could solve enterprise-wide problems. His book (with Jeff Alger) on object-oriented development, Developing Object-Oriented Software for the Macintosh (Addison Wesley, 1992), introduced the idea of scenarios and patterns to developers. He was an early advocate of the Microsoft .NET framework, and he successfully introduced it into many enterprises, including Charles Schwab. He was one of the earliest developers of Service Oriented Architecture (SOA), and as Senior Vice President of Advanced Technology and the Chief Architect at Charles Schwab, he built an integrated SOA solution that spanned the enterprise, from desktop PCs to servers to complex network mainframes. (He holds four patents as a result.) As one of IBM’s largest customers, he introduced the folks at IBM to SOA at the enterprise level and encouraged them to head in that direction.
Since the release of the iPhone SDK in March 2008, he has been focusing on mobile applications. He has had eight applications in the App Store. These include a series of Travel Photo Guides (developed with his partners at mobilefortytwo), and a Digital Field Guides series (http://lp.wileypub.com/DestinationDFGiPhoneApp), developed in partnership with John Wiley & Sons. He also has a free app called Expense Diary that allows you to keep track of things like expenses, mileage, and time by adding them to your calendar.
He has developed mobile strategies for a number of businesses, ranging from National Cinemedia to the American Automobile Association (AAA). His strategies focus on Mobile 2.0 — integrating mobile across the enterprise, creating a consistent user experience across devices and applications in an application ecosystem, and developing a user experience architecture that both leverages — and is constrained by — the device. He has spent the last three years working with mobile device users and developers to determine what makes mobile devices so appealing, what users want from an application on a phone or tablet, and what makes an app compelling. These efforts have resulted in the Application Ecosystem model for mobile applications and an underlying Model Application Controller Architecture based on web services that has become a key element in his client work and his books.
In his copious spare time, he also teaches introductory and advanced classes on iPhone and iPad development (for clients as well as some public classes) and does expert witness work.
Along with those apps and his consulting, he has written several books on iPhone programming, iPhone Application Development For Dummies (multiple editions) (Wiley), Objective-C For Dummies (Wiley), and he co-authored (with Tony Bove) iPad Application Development For Dummies (including multiple editions) (Wiley) and iPhone Application Development All-in-One For Dummies (Wiley). He’s also the primary author (with Jon Manning and Paris Buttfield-Addison) of iPhone & iPad Game Development For Dummies.
Karl Kowalski has traveled the world of computers and software development for far longer than he’s willing to admit. He has written programs for airplanes, robots, games, and even particle accelerators, and he has developed software on platforms ranging from desktop computers to mainframes all the way down to smartphones. He is also the author of Mac Application Development For Dummies (John Wiley & Sons, Inc., 2011). He lives near Boston and works for RSA, The Security Division of EMC, where he develops security solutions for mobile platforms such as iPhone and BlackBerry, and desktop operating systems such as Windows and Mac OS X. In his spare time, he develops software for smartphones as part of a startup, BlazingApps. And if there are any spare seconds in the day, he does some voice-over work for one of his favorite journals, The Objective Standard.
Dedication
To my friends and family, and especially my children Sarah and Evan and my wife, Linda. She deserves special recognition for her support and patience and for maintaining her (and my) sense of humor. Without her support I never would have been able to write 12 books in 3.5 years. Thank you so much.
This is for you As day follows night The tunnel ends in the light Finally time to play
— Neal Goldstein
To my incredible family: Connie, Stanley, Lee Anne, David, Rosemarie, Joseph, Candi, Reeseling the Nieceling, and Mason — thank you for all your love and support.
— Karl Kowalski
Author’s Acknowledgments
Acquisitions Editor Kyle Looper for extending the vision of this book and keeping this project on track. Thanks to Project Editor Charlotte Kughen for guiding us through the process and keeping everything on track and Copy Editor John Edwards for helping us make things clear. Tech Editor Jesse Feiler kept us on our toes, and his comments made this a better book. Great job, team, and I deeply appreciate all of your work.
Thanks again to my agent Carole Jelen for her continued work and support in putting and keeping these projects together.
— Neal Goldstein
Publisher’s Acknowledgments
We’re proud of this book; please send us your comments at http://dummies.custhelp.com. For other comments, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.
Some of the people who helped bring this book to market include the following:
Acquisitions and Editorial
Project Editor: Charlotte Kughen, The Wordsmithery LLC
Senior Acquisitions Editor: Kyle Looper
Copy Editor: John Edwards
Technical Editor: Jesse Feiler
Editorial Manager: Jodi Jensen
Editorial Assistant: Leslie Saxman
Sr. Editorial Assistant: Cherie Case
Cover Photo: © John Shepherd/iStockphoto.com
Cartoons: Rich Tennant (www.the5thwave.com)
Composition Services
Project Coordinator: Katherine Crocker
Layout and Graphics: Joyce Haughey, Julie Trippetti
Proofreaders: Lauren Mandelbaum, Bonnie Mikkelson
Indexer: BIM Indexing & Proofreading Services
Publishing and Editorial for Technology Dummies
Richard Swadley, Vice President and Executive Group Publisher
Andy Cummings, Vice President and Publisher
Mary Bednarek, Executive Acquisitions Director
Mary C. Corder, Editorial Director
Publishing for Consumer Dummies
Kathleen Nebenhaus, Vice President and Executive Publisher
Composition Services
Debbie Stailey, Director of Composition Services
Introduction
When the folks at John Wiley & Sons approached me about writing Objective-C Programming For Dummies, I thought long and hard about it. Within 480 pages, I wanted to be sure that I could explain to someone with no programming experience how to actually create useful programs.
So I started to think about what makes programming so difficult.
It isn’t the concept of how programs work, which I cover easily in Part I. And it isn’t really the language itself (or the instruction set — I cover that in Chapter 4). It isn’t even the user interface — all that code needed to open and close windows, process menus and the mouse and user touches, draw graphics, and play audio and video (did I leave anything out?). No, while all that used to be really hard, now it’s made much easier by using the frameworks available with Mac OS X and iOS.
What is really hard, after you understand the language and framework, is how you structure your program — how you actually go about taking your idea for an application and turning it into a robust Objective-C application.
Finding out how to use the tools is (relatively) easy; knowing how to use them to create a useful application is the real challenge.
So, besides explaining the instruction set and everything else involved with coding, what I do along the way is explain the other things you need to know (things like application architecture and design) — those things that will make it possible for you, when you are done with this book, to go out and start developing your first application. Nothing less.
So instead of a book that only shows you how to use all the features (instructions and frameworks) available to you, I decided to write a book that shows you both how and why. I do that by having you start to develop an application in Chapter 5 (after I go over the instruction set) and add to that same application until you end up with it running on both the iPhone and Mac in Chapters 17 and 18. Granted, this application isn’t the most exciting one in the world, but it gives you the opportunity to use every feature of Objective-C that you’ll need to know to go out and build your own killer app. What’s more, you build the application incrementally, just as a professional develops a commercial application. Occasionally, you will enter some code only to delete it later, which may seem annoying at times. However, you will get a flavor for how you’ll work when you are out on your own.
And while some development will be annoying and tedious, in general it is fun. So go enjoy yourself while you’re finding out about Objective-C. I know I do.
About This Book
Objective-C Programming For Dummies is a beginner’s guide to developing applications for both iOS devices and the Mac. You don’t need any programming experience to get started. I expect you to come as a blank slate, ready to be filled with useful information and new ways to do things. In some ways, the less you know, the easier it will be for you because you won’t have any preconceived notions about programming.
This book distills the hundreds (or even thousands) of pages of Apple documentation, not to mention my own development experience, into only what’s necessary to start you developing real applications. I explain not only the language, but also along the way, I explicitly talk about object-oriented principles and how doing things in a certain way (that is, following those principles) leads to more extensible and enhanceable programs, which you will discover is the holy grail of programming.
Conventions Used in This Book
This book guides you through the process of building applications by using Objective-C.
Code examples in this book appear in a monospaced font so that they stand out a bit better. This means that the code you see will look like this:
NSLog(@”I am an Objective-C statement.”);
Objective-C is based on C, which (I want to remind you) is case sensitive, so please enter the code that appears in this book exactly as it appears in the text. I also use the standard Objective-C naming conventions — for example, class names always start with a capital letter, and the names of methods and instance variables always start with a lowercase letter.
All URLs in this book appear in a monospaced font as well:
www.dummies.com
If you’re ever uncertain about anything in the code, you can always look at the source code at the website associated with this book: www.dummies.com/go/objcprogrammingfd. And from time to time, I provide updates for the code and post other things you might find useful on my website, www.nealgoldstein.com.
Foolish Assumptions
To find out how to program in Objective-C for Mac OS X or iOS, you need a Macintosh computer with the latest version of the Mac OS on it. You also need to download the Software Development Kit (SDK). You don’t have to become a registered Apple Developer to do that, but registering with Apple gives you access to all the information Apple provides for understanding Objective-C, iOS, and Mac OS X. I show you how to do both in Chapter 2 (don’t worry; it doesn’t cost a cent).
I assume that you don’t have any programming knowledge but that you have at least a passing acquaintance with some of the ideas, and more important, a desire to know how to program. In general, the code is easy and straightforward (the book isn’t written to dazzle you with fancy coding techniques).
I also assume that you’re familiar with the Mac and/or iPhone and that you are comfortable doing all the things you have to do on the Mac to run applications, including using the Finder to cruise the file system to see what’s there.
How This Book Is Organized
Objective-C Programming For Dummies has five main parts.
Part I: Getting to the Starting Line
Part I introduces you to the world of application development. You find out how programs work and what you have to do to take an idea and turn it into a computer program. I explain the tools that are available to you and describe how to use them, and I lead you through downloading the Software Development Kit (SDK), which includes Xcode (Apple’s development environment for the OS X and iOS operating systems). You get up and running on your first application, which gives you a taste for what words like compiling and building mean. You also find out how to become a registered Apple Developer, both for the Mac and the iPhone (and if you are an iPhone developer, what you are required to do to distribute your applications through Apple’s App Store).
Part II: Speaking the Language of Objective-C
As with any other skill, you have to pay your dues, and that means understanding the instruction set of the language and knowing how to use some of the languagelike features made available to you in the frameworks. You start by building an application that you will add to as you discover more and more about Objective-C.
Think of this as getting down the vocabulary of a new language, but without the pain and all that memorization.
Part III: Walking the Object-Oriented Walk
After you understand the basic instruction set and the other Objective-C and framework features, it’s time to put those instructions together to create a program. In this part, I focus on the right way to structure your program — what’s known as the program architecture. Having the right architecture results in a program that not only works but also can be extended to add new functionality easily. And not only that, it enables you to easily track down and fix those pesky bugs that make their home in everyone’s programs. I also show you how to deal with the mundane, but necessary, plumbing issues such as memory management and object initialization.
Whereas Part II is about getting down the vocabulary, Part III is about using the vocabulary to create sentences and paragraphs and even entire books.
Part IV: Moving from Language to Application
With an architecture in place, you can begin to add more and more functionality to your program. You start to work with data and discover some of the tricks that framework developers use to make their frameworks so extensible.
After you have your application doing what you want it to do, you need to take all that functionality and make it available to the user. So, in this part, I show you how your application fits into the user SDK-supplied frameworks that do all the user interface heavy lifting on Mac OS X and iOS. And because you design the application the right way from the start, you’ll be able to plug it into the user interface with minimal effort. You just do some building of the user interface in Interface Builder (part of the SDK), add a few lines of code, and you are there. No sweat, no bother. And yes, because you did it the right way from the start, the same application code will run on both the Mac and iOS devices (using the frameworks for Mac OS X and iOS).
Part V: The Part of Tens
Part V consists of voices from the trenches. I also show you some tips on debugging (yes, your application will, upon occasion, have bugs) that might shorten those late, into-the-night debugging sessions that are (unfortunately) part and parcel of being a developer. Although they may not always be fun, solved bugs are often a great source of conversation among developers. I also offer some tips about approaching application development that will lead to good health and happiness as a developer.
Icons Used in This Book
When you see this icon, you can be sure that the code available at this book’s website applies to the current example. You can find the code for all projects in this book at www.dummies.com/go/objcprogrammingfd — perfect for those who don’t feel like typing the code.
This icon indicates a useful pointer that you shouldn’t skip.
This icon represents a friendly reminder. It describes a vital point that you should keep in mind while proceeding through a particular section of the chapter.
This icon signifies that the accompanying explanation might be informative (dare I say, interesting), but it isn’t essential to understanding Objective-C application development. Feel free to skip past these tidbits if you like (though skipping while trying to absorb the main concepts may be tricky).
This icon alerts you to potential problems that you may encounter along the way. Read and obey these bits of experience to avoid trouble.
On the Web
I’ve uploaded all the code for the projects in this book to the website at www.dummies.com/go/objcprogrammingfd. You’ll find folders for each chapter starting with Chapter 4. In each of these chapter folders is a folder that contains the Xcode project that provides the starting point for that chapter. So for Chapter 4, for example, the folder is labeled Chapter 4 Start Here.
Also in the chapter folder is a folder that contains the final version of the project for each chapter (except for Chapter 4 where it isn’t applicable). Some chapters also have intermediate versions of the project; so, in Chapter 5, for example, the folders are Chapter 5 A, Chapter 5 B, and so on. I explain what is in each in the corresponding chapter.
Where to Go from Here
Occasionally, we have updates to our technology books. If this book does have technical updates, they will be posted at www.dummies.com/go/objcprogrammingfdupdates.
Please note that some special symbols used in this eBook may not display properly on all eReader devices. If you have trouble determining any symbol, please call Wiley Product Technical Support at 800-762-2974. Outside of the United States, please call 317-572-3993. You can also contact Wiley Product Technical Support at www.wiley.com/techsupport.
Part I
Getting to the Starting Line
In this part . . .
So you’ve decided you want to learn to program. You may have a good idea for a Mac or iPhone application and realize that the first thing you need to do is find out how to program in Objective-C. And although you may have a vague idea about it, you know you’re going to have to learn exactly what programming is and what’s required to create an application.
In this part, I help you understand what you need to know to get started. First of all, how do applications even work? How do you translate your ideas into a computer language that tells the computer what you want it to do, and then how does it take those instructions and actually do them? What is all this complier and framework stuff, and what exactly is object-oriented programming?
You find out what makes a good application and what you can do to make yours a good one. Finally, so you can get free development software from Apple, I take you through the process of registering as an Apple developer. I explain how you can download the Software Development Kit (SDK), and even how to build your first program.
Chapter 1
Computer Programming Exposed!
In This Chapter
Understanding the basics of computer programming
Getting how computer languages work
Knowing how Objective-C works
Looking at it from the outside, computer programming can appear complicated and a bit mysterious. But once I let you in on a few of the secrets, you’ll realize that when you write a computer program, whether it is a small program that’s just a few lines or one that is tens or even hundreds of thousands of lines, you are generally doing the same thing:
1. Getting input — from a keyboard or touch screen, or even something stored on your computer.
The input might be instructions to the program itself — for example, to display the web page https://developer.apple.com, to print a document such as Chapter 1, to process data like “Enter your Apple ID and Password” when you log on to the Mac Dev Center (the browser is just another program), or even to process a list of credit card transactions stored on a computer.
2. Doing something based on, or with, the input.
Your browser may go on the Internet and access the page corresponding to https://developer.apple.com, or your word processing program may display a Print dialog box and print the chapter (at least that is what mine does). Based on your input, the program may also go out and use data it has stored or even has access to over the Internet. For example, when you enter your Apple ID and password, eventually a computer accesses a database to see whether your Apple ID and password are both valid and, if so, allows you access to the site and displays the site for you.
3. Displaying the results of your adroitness on a monitor (or storing it away for future use).
Computers are no doubt engineering marvels. But what will make you a good programmer is not your understanding of all that wizardry. No, what will make you a good programmer is taking the time to really understand the world of the user, and what you can do with a computer to make things better. For example, when I travel, I often zone out on the fact that even though it looks like Monopoly money, foreign currency actually does amount to something in dollars. I could use a computer to keep track of my budget and convert foreign currency into dollars. Writing a program simply involves detailing the steps the computer needs to follow (in a language the computer understands — but I’ll get to that). You know, something like
Subtract the amount he just spent from the amount he started with.
or
Multiply the amount in foreign currency times the exchange rate.
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!