learn cocoa on the mac 2nd edition

409 2.4K 1
learn  cocoa  on  the  mac  2nd  edition

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

www.it-ebooks.info 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. www.it-ebooks.info v Contents at a Glance Preface ����������������������������������������������������������������������������������������������������������������������������� xvii About the Authors �������������������������������������������������������������������������������������������������������������� xix About the Technical Reviewer ������������������������������������������������������������������������������������������� xxi Acknowledgments ����������������������������������������������������������������������������������������������������������� xxiii Chapter 1: Must Love Cocoa ■ ����������������������������������������������������������������������������������������������1 Chapter 2: Hello, World ■ ������������������������������������������������������������������������������������������������������5 Chapter 3: Lights, Camera … Actions! (and Outlets, too) ■ �������������������������������������������������27 Chapter 4: The First Call to Action ■ �����������������������������������������������������������������������������������47 Chapter 5: GUI Components ■ ���������������������������������������������������������������������������������������������67 Chapter 6: Using Table Views ■ ����������������������������������������������������������������������������������������103 Chapter 7: Cocoa Bindings ■ ��������������������������������������������������������������������������������������������125 Chapter 8: Core Data Basics ■ ������������������������������������������������������������������������������������������153 Chapter 9: Core Data Relationships ■ �������������������������������������������������������������������������������189 Chapter 10: Search and Retrieve Core Data with Criteria ■ ���������������������������������������������209 Chapter 11: Windows and Menus and Sheets ■ ���������������������������������������������������������������225 Chapter 12: Document-Based Applications ■ �������������������������������������������������������������������253 www.it-ebooks.info vi Contents at a Glance Chapter 13: Exceptions, Signals, Errors, and Debugging ■ ����������������������������������������������271 Chapter 14: Drawing in Cocoa ■ ���������������������������������������������������������������������������������������295 Chapter 15: Advanced Drawing Topics ■ ��������������������������������������������������������������������������315 Chapter 16: Working with Files ■ �������������������������������������������������������������������������������������341 Chapter 17: Concurrency ■ �����������������������������������������������������������������������������������������������357 Chapter 18: Future Paths ■ �����������������������������������������������������������������������������������������������375 Index ���������������������������������������������������������������������������������������������������������������������������������389 www.it-ebooks.info 1 Chapter 1 Must Love Cocoa Welcome! You must be here because you want to write programs for your Mac. Well, you’ve definitely come to the right place. (Here for Pilates? Third door down, on the right.) By the time you finish this book, you’ll know everything you need to know to create fast, efficient, good-looking Mac OS X applications. The key to creating a modern Mac application is Cocoa. According to Apple, Cocoa is a set of object-oriented frameworks that provide a runtime environment for Mac OS X applications. As you make your way through this book, you’ll learn all about the Cocoa frameworks and runtime environment. For the moment, think of Cocoa as a programmer’s assistant that takes care of much of the housekeeping that goes along with Mac development. Almost every common task performed by a Mac application, from drawing a window to blinking the cursor in a text field, is handled for you when you write programs using Cocoa, freeing you up to concentrate on the code that makes your application unique. Cocoa provides a class for just about every one of your development needs. There are Cocoa classes for each piece of the Mac OS X user interface, from windows to menus, scrollbars to buttons, images to icons. If you can think of a user interface element you’d like to add to your own application, chances are very good that element is already implemented as a Cocoa class. Another benefit of using Cocoa is that it is tightly integrated with Mac OS X. Build your application using Cocoa, and your application will play well with others and will interface seamlessly with Mac OS X elements like the Finder and the Dock. WHEN IS 25 YEARS NOT 25 YEARS? Cocoa has been around in one form or another since 1986. The technologies that we call Cocoa evolved from the NeXTStep AppKit, the application building tools developed for the NeXT platform. When Apple bought NeXT in 1996, they began building a new version of the Mac OS, what we now know as OS X, basing much of the new operating system on technologies acquired from NeXT. www.it-ebooks.info 2 CHAPTER 1: Must Love Cocoa As OS X evolved, so did Cocoa. Apple added technologies from the classic Mac OS, like QuickTime, as well as completely new technologies, like the Quartz rendering system that enables all the fancy visual effects and animation that OS X uses. They also made sure that they kept the Mac’s famed ease-of-use in the process. Bottom line: Cocoa is constantly evolving and expanding. The development tools, libraries, and frameworks you’ll be learning in this book are the result of more than 25 years of experimentation and refinement. Get a Mac and Download the Tools Before you can begin creating applications with Cocoa, you’ll need a Mac. It doesn’t have to be the newest or the most powerful Mac; in fact, pretty much any Mac that’s been made in the last four or five years or so will work just fine for building the exercises in this book. As you become more serious about writing software, you may find it’s worth investing in a newer or faster machine, but for now just about any Intel-based Mac will work fine for learning what you need to learn. The latest Macs are currently up to OS X 10.8, while the oldest Intel-based Macs can’t run any version of OS X later than 10.7. But Xcode 4.6, the version we’ll be using for this book, works fine on both 10.7 and 10.8. You’ll also need to have an Apple ID (the same as you might already be using for the Mac or iOS App Stores, or other iTunes purchases), and you should strongly consider joining the Apple Developer Connection (ADC), which is Apple’s developer relations organization. There are paid memberships, but the basic membership is free. To join ADC, open up your web browser and navigate to http://developer.apple.com/mac/. If you already have an Apple ID, press the Log in button and enter your info. If you are new to Apple’s online services, click the register link and create your account. Whether you sign up as a developer or just use your Apple ID, the easiest and best way to get Xcode, Apple’s set of free developer tools, is to download it from the App Store on your Mac. Just launch the App Store, search for Xcode, and tell it to install. As with most of what’s on the App Store, installation is pretty much self-explanatory. Xcode’s installation may take quite a bit longer than most other apps since it’s a pretty big download. Note If you’ve already installed Xcode for doing iOS development, then you’re all set. Xcode contains all the tools and frameworks needed for creating both OS X apps and iOS apps. Though the frameworks do have major differences, the tools are pretty much the same, so if you’ve done any iOS development, you should feel right at home creating Cocoa programs. Download the Source Code In addition to downloading and installing Xcode, you’ll also need to download the sample projects that go with this book. You can find the code archive on the Apress Learn Cocoa on the Mac web page at www.apress.com/9781430245421. This page contains all the metadata about this book, including the code archive, errata, and more. www.it-ebooks.info 3CHAPTER 1: Must Love Cocoa Getting Help As you make your way through the book, you’ll no doubt have some questions you’d like to get answered. Fortunately, there is a great community you can join, designed specifically for this series of books. Check out http://learncocoa.org. Register with the site, find the appropriate book forum and post your questions. You are sure to find other folks who can answer your questions and, who knows, after a while you might find yourself answering questions for other folks as well. The learncocoa.org site is also where you’ll find some info about other resources to help you on your way, including other books, workshops, and more. Come join us! What You Need to Know Before You Begin This book assumes that you already have some basic programming knowledge. You should be comfortable with the concepts of loops, variables, pointers, and linked lists. This book also assumes that you understand the fundamentals of object-oriented programming and are familiar with the Objective-C programming language. Starting with Mac OS X Leopard 10.5, a new version of the language was introduced, called Objective-C 2.0. Don’t worry if you’re not familiar with the more recent additions to the Objective-C language. We’ll be sure to highlight any of the 2.0 language features we take advantage of and explain how they work and why we are using them. NEW TO OBJECTIVE-C? Here are a few resources that will help you get started. New to programming? Check out Learn C on the Mac, 4th Edition, written by our good friend Dave Mark (www.apress.com/9781430218098). New to Objective-C? Check out Learn Objective-C on the Mac, 2nd Edition, an excellent and approachable introduction to Objective-C by Mac programming experts Scott Knaster, Waqar Malik, and Mark Dalrymple (www.apress.com/9781430241881). Next, navigate over to Apple’s Mac Developer Library and download a copy of Programming with Objective-C, a very detailed and extensive description of the language and a great reference guide (http://developer.apple.com/ library/mac/#documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC). Are You Ready? The Mac is a great computing platform, and Cocoa is the best tool to use to build your Mac applications. If you are coming to the Mac from another platform, you may find working with Cocoa and Objective-C a bit disorienting. Even though you might find it hard to get your bearings at first, as you make your way through this book, we’re confident that you’ll soon start to wrap your head around the “Cocoa way” of doing things. With a little perseverance, it will all start to make sense. www.it-ebooks.info 4 CHAPTER 1: Must Love Cocoa One thing you should keep in mind is that the exercises in this book are not simply a checklist that, when completed, grant you Cocoa developer guru status. Every step of the way, make sure you understand what you just did before moving on to the next project. Don’t be afraid to make changes to the code. Experiment and observe the results. That’s one of the best ways to understand the complexities of coding in an environment like Cocoa. Also, remember that we’ve set up a message forum for readers to discuss the exercises and content of this book. So if you get stuck, or want to chat with us and other Cocoa developers about any Cocoa programming topics, head over to http://learncocoa.org and register for the discussion forum. That said, if you’ve got Xcode installed, turn the page. If not, get to it! Then let’s go! www.it-ebooks.info 5 Chapter 2 Hello, World As you’re probably well aware, it has become something of a tradition to call the first project in any book on programming “Hello, World.” Following the “if it ain’t broke, don’t fix it” guideline, we’ll stick with tradition. Building “Hello, World” By now, you should have Xcode installed on your machine. You should also have the Learn Cocoa Projects folder ensconced safely somewhere on your hard drive. If by some set of circumstances you don’t, go directly to Chapter 1 (do not pass Go, do not collect $200) and reread the appropriate sections. The first project we’ll be working with is located in the Chapter02/Chapter2 folder. Launch Xcode, which is located in the Applications folder. Just in case you’ve never used Xcode before, we’re going to walk you through the process of creating a new project. Start by selecting New Project from the File menu, or by typing ⇧⌘N. When the New Project Assistant comes up (Figure 2-1), select Application from under the Mac OS X heading in the left column, then select the Cocoa Application icon from the upper-right pane and click Next. www.it-ebooks.info 6 CHAPTER 2: Hello, World A Project Options window offers a number of choices (Figure 2-2). We need to specify a name for our application. “Chapter2” is a good choice. We also need to give a company identifier, which is formatted like a reversed domain name (much like a Java package name or C# namespace). If we live at the megacorp.com domain name, then com.megacorp would be our choice here. Mac OS X uses the combination of the product name and the company identifier as a unique name for our program across the system, as well as in the Mac App Store. However, since we won’t be distributing this app beyond our computer, we can pick anything we like. The last important field here is the class prefix. As we’ll see, many of the Cocoa classes start with the prefix NS or CF. In those classes, Xcode will generate some files for us as part of creating a new project, and it will use the prefix we give here. For now, enter “Book.” We won’t be using the other fields in this chapter, so they can be left at the default values. Click Next. Figure 2-1. Selecting the Cocoa Application project template from Xcode’s New Project Assistant www.it-ebooks.info [...]... program is running, or to step through the source code line by line using the debugger When we click the Run button, we’re getting the Debug configuration The Archive build process, on the other hand, uses the Release configuration The Release configuration doesn’t include debugging information, and it does more optimization of the resulting program It can also be configured to do multi-architecture builds... directions Certain items, like labels, have only two resize handles The label’s attributes (the size of its font in particular) determine the vertical size of the label We don’t change the height of the label by resizing it We only use the resize handles to change the width of the label Let’s center the label Make sure the left side of the label is lined up with the blue guideline near the left edge of the. .. Chapter 2 If you’d rather roll your own, go right ahead, using the same naming conventions in the hello world.iconset folder for your images Adding an Icon to our Project Whether you created an icon yourself or you are using ours, it’s time to add the icon to the project in Xcode To do this, select the Project in the Navigator area on the left side of the screen It’ll be the top item in the Project Navigator... functionality into the following three distinct categories:  Model: The classes that hold your application’s data  View: The windows, controls, and other elements that the user can see and interact with  Controller: The part that binds the model and view together and contains the application logic that determines how to handle the user’s inputs The goal of MVC is to make the objects that implement these... clicking the leftmost icon in the View section of the toolbar We’re going to be focusing on the Utility area on the right To start with, look at the Object Library pane at the bottom of the right side of the Xcode window Scroll through the list of objects to find Label; it should be about a dozen objects down from the top of the list Click Label in the Object Library and then drag it out to the upper... in the file system, localization information, plus the build targets that use the selected file, encoding information, and version control info Other files show information appropriate to the file type Depending on the type of file selected in the Navigator pane, there may be a row of small icons atop this area that allow us to select several different views of information to be shown here Below the. .. access to the default fonts Press ^⇧⌘T to bring up the font window in Xcode Make sure that our label is still selected (look for the resize handles), and also make sure that our application’s main window is still the front-most window Once we have our label looking just the way we like, we’ll put some finishing touches to our application, then run it We’re almost there! Creating the Application Icon One... that the row of icons across the top of the Utility area, along the right side of the Xcode window, has expanded from two to eight; these objects are more configurable than files are Each of these icons represents a different mode for the Inspector We’ll have more to say about the different types of information on display here shortly Underneath this is the Library, with a row of four small icons that... move the object near the edges, blue lines appear Note  One of the things that has made using the Mac such a pleasant experience over the years has been the consistency of the user interface In the vast majority of Mac applications, you can count on the ability to press ⌘W to close a window, ⌘S to save, and ⌘P to print, regardless of what program you are in If you’re going to write software for the Mac, ... area takes up the bottom third or so of the Utility area on the right-hand side of the Xcode window Another important Interface Builder tool is situated right above the Library, taking up the upper two-thirds of the Utility area on the right This is called the Inspector The Inspector is a context-sensitive pane that displays information about the currently selected object Click a window and the Inspector . Check out Learn C on the Mac, 4th Edition, written by our good friend Dave Mark (www.apress.com/9781430218098). New to Objective-C? Check out Learn Objective-C on the Mac, 2nd Edition, an excellent. information, the full path to the file in the file system, localization information, plus the build targets that use the selected file, encoding information, and version control info. Other files. for the Mac or iOS App Stores, or other iTunes purchases), and you should strongly consider joining the Apple Developer Connection (ADC), which is Apple’s developer relations organization. There

Ngày đăng: 05/05/2014, 14:42

Từ khóa liên quan

Mục lục

  • Learn Cocoa on the Mac

    • Contents at a Glance

    • Contents

    • Preface

    • About the Authors

    • About the Technical Reviewer

    • Acknowledgments

    • Chapter 1: Must Love Cocoa

      • Get a Mac and Download the Tools

      • Download the Source Code

      • Getting Help

      • What You Need to Know Before You Begin

      • Are You Ready?

      • Chapter 2: Hello, World

        • Building “Hello, World”

        • Exploring the Nib File

          • The Library

          • Dragging Out a Label

          • Using the Blue Guidelines

          • The Inspector

          • The Attributes Inspector

          • Change the Label’s Color and Font

          • Creating the Application Icon

          • Adding an Icon to our Project

Tài liệu cùng người dùng

Tài liệu liên quan