For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. Contents at a Glance Contents .............................................................................................................. v About the Authors ............................................................................................... x About the Technical Reviewer ........................................................................... xi Acknowledgments ............................................................................................ xii Introduction ..................................................................................................... xiii(cid:1) ■Chapter 1: Becoming a Great iOS or Mac Programmer ................................... 1(cid:1) ■Chapter 2: Programming Basics ................................................................... 13(cid:1) ■Chapter 3: It’s All About the Data .................................................................. 39(cid:1) ■Chapter 4: Making Decisions About…and Planning Program Flow .............. 63(cid:1) ■Chapter 5: Object Oriented Programming with Objective-C .......................... 87(cid:1) ■Chapter 6: Learning Objective-C and Xcode ................................................ 103(cid:1) ■Chapter 7: Objective-C Classes, Objects, and Methods ............................... 129(cid:1) ■Chapter 8: Programming Basics in Objective-C .......................................... 163(cid:1) ■Chapter 9: Comparing Data ......................................................................... 199(cid:1) ■Chapter 10: Creating User Interfaces .......................................................... 215(cid:1) ■Chapter 11: Storing Information .................................................................. 237(cid:1) ■Chapter 12: Protocols and Delegates .......................................................... 261(cid:1) ■Chapter 13: Memory, Addresses, and Pointers ........................................... 267(cid:1) ■Chapter 14: Introducing the Xcode Debugger ............................................. 291(cid:1) Index ............................................................................................................... 309 iv Introduction Over the last three years, we’ve heard the following countless times: ■ “I’ve never programmed before, but I have a great idea for an iPhone/iPad app.” ■ “Can I really learn to program the iPhone or iPad?” We always answer, “Yes, but you have to believe you can.” Only you are going to tell yourself you can’t do it. For the Newbie This book assumes you may have never programmed before. The book is also written for someone who may have never programmed before using object-oriented programming (OOP) languages. There are many Objective-C books out there, but all of these books assume you have programmed before and know OOP and computer logic. We wanted to write a book that takes readers from knowing little or nothing about computer programming and logic to being able to program in Objective-C. After all, Objective-C is the native programming language for the iPhone, iPad, and Mac. Over the last three years, we have taught well over a thousand students at xcelMe.com to be iPhone/iPad (iOS) developers. Many of our students have developed some of the most successful iOS apps in their category in the iTunes App Store. We have incorporated what we have learned in our first two courses, Introduction to Object-oriented Programming and Logic and Objective-C for iPhone/iPad Developers, into this book. For the More Experienced Many developers who programmed years ago or programmed in a non-OOP language need a background in OOP and Logic before they dive into Objective-C. This book is for you. We gently walk you through OOP and how it is used in iOS development to help make you a successful iOS developer. Why Alice: An Innovative 3D Programming Environment Over the years, universities have struggled with several issues with their computer science departments: ■ High male-to-female ratios ■ High drop-out rates xiii ■ INTRODUCTION ■ Longer than average time to graduation One of the biggest challenges to learning OOP languages like Java, C++, or Objective-C is the steep learning curve from the very beginning. In the past, students had to learn the following topics all at once: ■ Object-oriented principles ■ A complex Integrated Development Environment (IDE), i.e., Xcode, Eclipse, Visual Studio ■ The syntax of the programming language ■ Programming logic and principles As a result, Carnegie Mellon University received a grant from the US government and developed Alice. Alice, an innovative 3D programming environment, makes it easy for new developers to create rich graphical applications. Alice is a teaching tool for students learning to program in an OOP environment. The software uses 3D graphics and a drag-and-drop interface to facilitate a more engaging, less frustrating first programming experience. Alice enables students to focus on learning the principles of OOP without having to focus on learning a complex IDE and Objective-C principles all at once. You get to focus on each topic individually. This helps students feel a real sense of accomplishment as they progress. As drag-and-drop programming, Alice removes all the complexity of learning an IDE and programming language syntax. You’ll see programming is actually fun, and you can develop very cool and sophisticated apps in Alice. After we introduce the OOP topic and readers feel comfortable with the material, we then move into Xcode, where you get to use your new OOP knowledge in writing Objective-C applications. This way, you can focus on the Objective-C syntax and language without having to learn OOP at the same time. Learning Objective-C Without Alice More than a thousand xcelMe.com students have used this book to become successful iOS developers. At the end of each course, we ask our students if the Alice sections in the first four sections were useful. More than half of the students thought using Alice at the beginning of the first four chapters to introduce the chapter was critical to their success. However, some of the students didn’t feel they needed the Alice examples at the beginning of the first four chapters. We have laid out the first four chapters of this book with the first part of each chapter introducing the OOP topic with Alice; the remaining part of the chapter introduces the topic using Objective-C. Thus, you can skip the Alice material if you feel comfortable with the topic. How This Book Is Organized You’ll notice that we are all about successes in this book. We introduce the OOP and Logic concepts in Alice and then move those concepts to Xcode and Objective-C. Many students are visual or learn by doing. We use both techniques. We’ll walk you through topics and concepts with visual examples and then take you through step-by-step examples reinforcing the concepts. We often repeat topics in different chapters to reinforce what you have learned and apply these skills in new ways. This enables new programmers to reapply development skills and feel a sense of accomplishment as they progress. Don’t worry if you feel you haven’t mastered a topic. Keep moving forward! xiv ■ INTRODUCTION The Formula for Success Learning to program is an interactive process between your program and you. Just like learning to play an instrument, you have to practice. You must work through the examples and exercises in this book. Understanding the concept doesn’t mean you know how to apply it and use it. You will learn a lot from this book. You will learn a lot from working through the exercises in this book. However, you will really learn when you debug your programs. Spending time walking through your code and trying to find out why it is not working the way you want is an unparalleled learning process. The downside of debugging is a new developer can find it especially frustrating. If you have never wanted to throw your computer out the window, you will. You will question why you are doing this, and whether you are smart enough to solve the problem. Programming is very humbling, even for the most experienced developer. Like a musician, the more you practice the better you get. By practicing, we mean programming! You can do some amazing things as a programmer. The world is your oyster. Seeing your app in the iTunes App Store is one of the most satisfying accomplishments. However, there is a price, and that price is time spent coding and learning. Having taught more than a thousand students to become iOS developers, we have put together a formula for what makes students successful. Here is our formula for success: ■ Believe you can do it. You’ll be the only one who says you can’t do this. So don’t tell yourself that. ■ Work through all the examples and exercises in this book. ■ Code, code, and keeping coding. The more you code, the better you’ll get. ■ Be patient with yourself. If you were fortunate enough to have been a 4.0 student who can memorize material just by reading it, this will not happen with Objective-C coding. You are going to have to spend time coding. ■ You learn by reading this book. You really learn by debugging your code. ■ Use the free xcelMe.com webinars and YouTube videos explained at the end of this chapter. ■ Don’t give up! The Development Technology Stack We will walk you through the process of understanding the development process for your iOS apps and what technology you need. However, briefly looking at all the pieces together is helpful. For a sample iPhone app in a Table View, see Figure 1. xv ■ INTRODUCTION Figure 1. The iPhone/iPad technology stack Required Software, Materials, and Equipment One of the great things about Alice is it available on the three main operating systems used today: ■ Windows ■ Mac ■ Linux The other great thing about Alice is it is free! You can download Alice at www.Alice.org. Operating System and IDE Although you can use Alice on many platforms, the Integrated Development Environment (IDE) that developers use to develop iOS apps is Xcode. You have to use an Intel-based Mac to use Xcode and submit apps! Xcode is free and is available in the Mac App Store. xvi ■ INTRODUCTION Software Development Kits You will need to register as an iOS developer. You can do this at http://developer.apple.com/iphone. When you are ready to upload your app to the iTunes App Store, you will need to pay $99/year. Dual Monitors We recommend developers have a second monitor connected to their computer. It is great to step through your code and watch your output window and iPad simulator at the same time on dual independent monitors. Apple hardware makes this easy. Just plug your second monitor into the display port of any Intel-based Mac, with the correct Mini DisplayPort adapter of course, and you have two monitors working independently of one another. See Figure 2. Note that dual monitors are not required. You will just have to organize your open windows to fit on your screen if you don’t. Figure 2. Dual monitors Free Live Webinars, Q&A, and YouTube Videos Nearly every Wednesday night at 7:30 p.m. Pacific daylight time, we have live webinars and discuss a topic from the book or a timely item of interest. These webinars are free, and you can register for them at www.xcelme.com/free-webinars.php. At the end of the webinars, we do a Q&A. You can ask a question on the topic discussed or any topic in the book. xvii ■ INTRODUCTION Additionally, all these webinars are recorded and available on YouTube. Make sure you subscribe to the YouTube channel so you are notified when new recordings are uploaded. Figure 3. Free Objective-C webinars and YouTube videos Free Book Forum We have developed an online forum for this book at http://forum.xcelme.com, where you can ask questions while you are learning Objective-C and get answers from the authors. You will also find answers to the exercises and additional exercises to help you learn. See Figure 3. You can also access answers to exercises and discover helpful links to help you become a successful iPhone/iPad developers and create great apps. See Figure 4. So let’s get started! xviii ■ INTRODUCTION Figure 4. Reader Forum for accessing answers to exercise and posting questions for authors xix
Description: