Cocoa Programming for Mac OS X

The best-selling introduction to Cocoa, once again updated to cover the latest Mac programming technologies, and still enthusiastically recommended by experienced Mac OS X developers. 

“Cocoa® Programming for Mac® OS X is considered by most to be the de-facto intro-to-OS X programming text.”

—Bob Rudis, the Apple Blog

 

“I would highly recommend this title to anyone interested in Mac development. Even if you own the previous edition, I think you’ll find the new and revised content well worth the price.”

—Bob McCune, bobmccune.com 

If you’re developing applications for Mac OS X, Cocoa® Programming for Mac® OS X, Fourth Edition, is the book you’ve been waiting to get your hands on. If you’re new to the Mac environment, it’s probably the book you’ve been told to read first.

 

Covering the bulk of what you need to know to develop full-featured applications for OS X, written in an engaging tutorial style, and thoroughly class-tested to assure clarity and accuracy, it is an invaluable resource for any Mac programmer. Specifically, Aaron Hillegass and Adam Preble introduce the two most commonly used Mac developer tools: Xcode and Instruments. They also cover the Objective-C language and the major design patterns of Cocoa. Aaron and Adam illustrate their explanations with exemplary code, written in the idioms of the Cocoa community, to show you how Mac programs should be written. After reading this book, you will know enough to understand and utilize Apple’s online documentation for your own unique needs. And you will know enough to write your own stylish code.

 

Updated for Mac OS X 10.6 and 10.7, this fourth edition includes coverage of Xcode 4, blocks, view-based table views, Apple’s new approach to memory management (Automatic Reference Counting), and the Mac App Store. This edition adds a new chapter on concurrency and expands coverage of Core Animation. The book now devotes a full chapter to the basics of iOS development.

1100622524
Cocoa Programming for Mac OS X

The best-selling introduction to Cocoa, once again updated to cover the latest Mac programming technologies, and still enthusiastically recommended by experienced Mac OS X developers. 

“Cocoa® Programming for Mac® OS X is considered by most to be the de-facto intro-to-OS X programming text.”

—Bob Rudis, the Apple Blog

 

“I would highly recommend this title to anyone interested in Mac development. Even if you own the previous edition, I think you’ll find the new and revised content well worth the price.”

—Bob McCune, bobmccune.com 

If you’re developing applications for Mac OS X, Cocoa® Programming for Mac® OS X, Fourth Edition, is the book you’ve been waiting to get your hands on. If you’re new to the Mac environment, it’s probably the book you’ve been told to read first.

 

Covering the bulk of what you need to know to develop full-featured applications for OS X, written in an engaging tutorial style, and thoroughly class-tested to assure clarity and accuracy, it is an invaluable resource for any Mac programmer. Specifically, Aaron Hillegass and Adam Preble introduce the two most commonly used Mac developer tools: Xcode and Instruments. They also cover the Objective-C language and the major design patterns of Cocoa. Aaron and Adam illustrate their explanations with exemplary code, written in the idioms of the Cocoa community, to show you how Mac programs should be written. After reading this book, you will know enough to understand and utilize Apple’s online documentation for your own unique needs. And you will know enough to write your own stylish code.

 

Updated for Mac OS X 10.6 and 10.7, this fourth edition includes coverage of Xcode 4, blocks, view-based table views, Apple’s new approach to memory management (Automatic Reference Counting), and the Mac App Store. This edition adds a new chapter on concurrency and expands coverage of Core Animation. The book now devotes a full chapter to the basics of iOS development.

23.49 In Stock
Cocoa Programming for Mac OS X

Cocoa Programming for Mac OS X

by Aaron Hillegass, Adam Preble
Cocoa Programming for Mac OS X

Cocoa Programming for Mac OS X

by Aaron Hillegass, Adam Preble

eBook

$23.49  $39.99 Save 41% Current price is $23.49, Original price is $39.99. You Save 41%.

Available on Compatible NOOK devices, the free NOOK App and in My Digital Library.
WANT A NOOK?  Explore Now

Related collections and offers


Overview

The best-selling introduction to Cocoa, once again updated to cover the latest Mac programming technologies, and still enthusiastically recommended by experienced Mac OS X developers. 

“Cocoa® Programming for Mac® OS X is considered by most to be the de-facto intro-to-OS X programming text.”

—Bob Rudis, the Apple Blog

 

“I would highly recommend this title to anyone interested in Mac development. Even if you own the previous edition, I think you’ll find the new and revised content well worth the price.”

—Bob McCune, bobmccune.com 

If you’re developing applications for Mac OS X, Cocoa® Programming for Mac® OS X, Fourth Edition, is the book you’ve been waiting to get your hands on. If you’re new to the Mac environment, it’s probably the book you’ve been told to read first.

 

Covering the bulk of what you need to know to develop full-featured applications for OS X, written in an engaging tutorial style, and thoroughly class-tested to assure clarity and accuracy, it is an invaluable resource for any Mac programmer. Specifically, Aaron Hillegass and Adam Preble introduce the two most commonly used Mac developer tools: Xcode and Instruments. They also cover the Objective-C language and the major design patterns of Cocoa. Aaron and Adam illustrate their explanations with exemplary code, written in the idioms of the Cocoa community, to show you how Mac programs should be written. After reading this book, you will know enough to understand and utilize Apple’s online documentation for your own unique needs. And you will know enough to write your own stylish code.

 

Updated for Mac OS X 10.6 and 10.7, this fourth edition includes coverage of Xcode 4, blocks, view-based table views, Apple’s new approach to memory management (Automatic Reference Counting), and the Mac App Store. This edition adds a new chapter on concurrency and expands coverage of Core Animation. The book now devotes a full chapter to the basics of iOS development.


Product Details

ISBN-13: 9780132902205
Publisher: Pearson Education
Publication date: 11/09/2011
Sold by: Barnes & Noble
Format: eBook
Pages: 528
File size: 69 MB
Note: This product may take a few minutes to download.

About the Author

Aaron Hillegass, who worked at NeXT and Apple, now teaches popular Cocoa programming classes at Big Nerd Ranch. At NeXT, he wrote the first course on OpenStep, the predecessor to today’s Cocoa tools. This book is based on the big Nerd Ranch course and is influenced by more than a decade of work with OpenStep and Cocoa.

 

Adam Preble learned Cocoa programming from the first edition of this book. After too many years of professional C/C++ development, today Adam writes Mac and iOS applications at Big Nerd Ranch, where he is also a Cocoa instructor. He is frequently filling in the gaps between work and family time with pinball machine software development and countless other projects.

Read an Excerpt

Chapter 12: Custom Views

All the visible objects in an application are either windows or views. In this chapter, you will create a subclass of NSView . From time to time, you will create a custom view to do custom drawing or event handling. Even if you do not plan to do custom drawing or event handling, by learning how to create a new view class, you will learn a lot about how Cocoa works.

Windows are instances of the class NSWindow . Each window has a collection of views. Each view is responsible for a rectangle of the window. The view draws inside that rectangle and handles mouse events that occur there. A view may also handle keyboard events. You have worked with several subclasses of NSView already: NSButton, NSTextField, NSTableView, and NSColorWell are all views. (Note that a window is not a subclass of NSView .)

The View Hierarchy

Views are arranged in a hierarchy (Figure 12.1). The window has a content view that completely fills its interior. The content view usually has several subviews. Each subview may have subviews of its own. Every view knows its superview, its subviews, and the window it lives on.

Here are the relevant methods:

  • (NSView *)superview ;
  • (NSArray *)subviews ;
  • (NSWindow *)window ;
Any view can have subviews, but most don't. Here are five views that commonly have subviews.
  • The content view of a window.
  • NSBox . The contents of a box are its subviews.
  • NSScrollView . If a view appears in a scroll view, it is a subview of the scroll view. The scroll bars are also subviews of the scroll view.
  • NSSplitView . Each view in a split view is a subview (Figure 12.2).
  • NSTabView . As the user chooses different tabs, different subviews are swapped in and out (Figure 12.3).

Get a View to Draw Itself

In this section, you are going to create a very simple view. It will simply appear and paint itself green. It will look like Figure 12.4.

Create a new project of type Cocoa Application (Figure 12.5).

Name it ImageFun

After the new project is created, open MainMenu. nib, and select NSView in the classes browser (Figure 12.6).

Press return to create a subclass, and name it StretchView (Figure 12.7).

Create the files for StretchView (Figure 12.8).

Save the files in the project directory.

Create an Instance of a View Subclass

Now create an instance of your class by dragging out a CustomView placeholder and dropping it on the window (Figure 12.9).

Resize the view to fill most of the window. Open the info panel and set the class of the view to be StretchView (Figure 12.10).

Notice that creating an instance of a view is different from creating an instance of a controller object like AppController . To create an instance of AppController in Chapter 7, you used the Instantiate menu item. When creating a view, it is important that you attach it to a window and give it a size and location in that window.

Size Info

Your StretchView object is a subview of the window's content view. An interesting question is: What happens to the view when the superview resizes? There is a page in the info panel that allows you to set that behavior. Open the size info panel, and set it as shown in Figure 12.11. This means that it will grow and shrink as necessary to keep the distance from its edges to the edges of its superview constant.

If you wanted the view to stay the same size, you could let the distance between the edges of the view and the edges of the superview grow and shrink. In this exercise, you do not want this behavior. But in a parallel universe where you did, the inspector would look like this (Figure 12.12).

Save and close the nib file.

drawRect:

When a view needs to draw itself, it is sent the message drawRect: with the rectangle that needs to be drawn or redrawn. This method is called automatically, and you will never need to call it directly. Instead, if you know that a view needs redrawing, you will send the view the message setNeedsDisplay...

Table of Contents

Preface xix

Acknowledgments xxi

 

Chapter 1: Cocoa: What Is It? 1

A Little History 1

Tools 3

Language 4

Objects, Classes, Methods, and Messages 5

Frameworks 6

How to Read This Book 7

Typographical Conventions 7

Common Mistakes 8

How to Learn 8

 

Chapter 2: Let’s Get Started 11

In Xcode 11

In Interface Builder 15

A Look at Objective-C 25

Documentation 31

What Have You Done? 31

Chronology of an Application 32

 

Chapter 3: Objective-C 35

Creating and Using Instances 35

Using Existing Classes 37

Creating Your Own Classes 48

The Debugger 58

What Have You Done? 63

Meet the Static Analyzer 63

For the More Curious: How Does Messaging Work? 65

Challenge 66

 

Chapter 4: Memory Management 67

Living with Manual Reference Counting 69

Accessor Methods 77

Living with ARC 80

 

Chapter 5: Target/Action 83

Some Commonly Used Subclasses of NSControl 85

Start the SpeakLine Example 89

Lay Out the XIB File 90

Implementing the SpeakLineAppDelegate Class 94

For the More Curious: Setting the Target Programmatically 96

Challenge 96

Debugging Hints 98

 

Chapter 6: Helper Objects 99

Delegates 100

The NSTableView and Its dataSource 104

Lay Out the User Interface 107

Make Connections 109

Edit SpeakLineAppDelegate.m 110

For the More Curious: How Delegates Work 113

Challenge: Make a Delegate 114

Challenge: Make a Data Source 114

 

Chapter 7: Key-Value Coding and Key-Value Observing 117

Key-Value Coding 117

Bindings 119

Key-Value Observing 120

Making Keys Observable 121

Properties 124

For the More Curious: Key Paths 126

For the More Curious: Key-Value Observing 127

 

Chapter 8: NSArrayController 129

Starting the RaiseMan Application 130

Key-Value Coding and nil 139

Add Sorting 140

For the More Curious: Sorting without NSArrayController 141

Challenge 1 142

Challenge 2 142

 

Chapter 9: NSUndoManager 145

NSInvocation 145

How the NSUndoManager Works 146

Adding Undo to RaiseMan 148

Key-Value Observing 152

Undo for Edits 153

Begin Editing on Insert 156

For the More Curious: Windows and the Undo Manager 158

 

Chapter 10: Archiving 159

NSCoder and NSCoding 160

The Document Architecture 163

Saving and NSKeyedArchiver 167

Loading and NSKeyedUnarchiver 168

Setting the Extension and Icon for the File Type 170

For the More Curious: Preventing Infinite Loops 172

For the More Curious: Creating a Protocol 173

For the More Curious: Automatic Document Saving 174

For the More Curious: Document-Based Applications without Undo 175

Universal Type Identifiers 175

 

Chapter 11: Basic Core Data 177

NSManagedObjectModel 177

Interface 179

For the More Curious: View-Based versus Cell-Based Table Views 191

Challenge 191

 

Chapter 12: NIB Files and NSWindowController 193

NSPanel 193

Adding a Panel to the Application 194

For the More Curious: NSBundle 204

Challenge 206

 

Chapter 13: User Defaults 207

NSDictionary and NSMutableDictionary 208

NSUserDefaults 210

Setting Defaults 212

Letting the User Edit the Defaults 213

Using the Defaults 215

For the More Curious: NSUserDefaultsController 217

For the More Curious: Reading and Writing Defaults from the Command Line 217

Challenge 219

 

Chapter 14: Using Notifications 221

What Notifications Are and Are Not 221

What Notifications Are Not 222

NSNotification 222

NSNotificationCenter 222

Posting a Notification 224

Registering as an Observer 225

Handling the Notification When It Arrives 226

The userInfo Dictionary 226

For the More Curious: Delegates and Notifications 227

Challenge 228

 

Chapter 15: Using Alert Panels 229

Make the User Confirm the Deletion 230

Challenge 232

 

Chapter 16: Localization 233

Localizing a NIB File 234

String Tables 236

For the More Curious: ibtool 239

For the More Curious: Explicit Ordering of Tokens in Format Strings 240

 

Chapter 17: Custom Views 241

The View Hierarchy 241

Get a View to Draw Itself 243

Drawing with NSBezierPath 248

NSScrollView 250

Creating Views Programmatically 252

For the More Curious: Cells 253

For the More Curious: isFlipped 255

Challenge 255

 

Chapter 18: Images and Mouse Events 257

NSResponder 257

NSEvent 257

Getting Mouse Events 259

Using NSOpenPanel 259

Composite an Image onto Your View 264

The View’s Coordinate System 266

Autoscrolling 268

For the More Curious: NSImage 269

Challenge 270

 

Chapter 19: Keyboard Events 271

NSResponder 273

NSEvent 273

Create a New Project with a Custom View 274

For the More Curious: Rollovers 282

The Fuzzy Blue Box 284

 

Chapter 20: Drawing Text with Attributes 285

NSFont 285

NSAttributedString 286

Drawing Strings and Attributed Strings 289

Making Letters Appear 289

Getting Your View to Generate PDF Data 291

For the More Curious: NSFontManager 293

Challenge 1 293

Challenge 2 294

 

Chapter 21: Pasteboards and Nil-Targeted Actions 295

NSPasteboard 296

Add Cut, Copy, and Paste to BigLetterView 298

Nil-Targeted Actions 300

For the More Curious: Which Object Sends the Action Message? 303

For the More Curious: UTIs and the Pasteboard 303

For the More Curious: Lazy Copying 304

Challenge 1 305

Challenge 2 305

 

Chapter 22: Categories 307

Add a Method to NSString 307

For the More Curious: Declaring Private Methods 309

 

Chapter 23: Drag-and-Drop 311

Make BigLetterView a Drag Source 312

Make BigLetterView a Drag Destination 315

For the More Curious: Operation Mask 319

 

Chapter 24: NSTimer 321

Lay Out the Interface 323

Make Connections 325

Add Code to TutorController 326

For the More Curious: NSRunLoop 328

Challenge 328

 

Chapter 25: Sheets 329

Adding a Sheet 330

For the More Curious: contextInfo 335

For the More Curious: Modal Windows 336

 

Chapter 26: Creating NSFormatters 339

A Basic Formatter 341

The Delegate of the NSControl Class 347

Checking Partial Strings 348

Formatters That Return Attributed Strings 350

For the More Curious: NSValueTransformer 351

 

Chapter 27: Printing 353

Dealing with Pagination 353

For the More Curious: Are You Drawing to the Screen? 358

Challenge 358

 

Chapter 28: Web Services 359

RanchForecast Project 360

Opening URLs 368

Challenge: Add a WebView 369

 

Chapter 29: Blocks 371

Block Syntax 373

Challenge: Design a Delegate 381

 

Chapter 30: Developing for iOS 383

Porting RanchForecast to iOS 383

RootViewController 386

Add a Navigation Controller 388

ScheduleViewController 391

UITableViewController 392

Pushing View Controllers 393

Challenge 395

 

Chapter 31: View Swapping 397

Get Started 398

Add View Swapping to MyDocument 401

Resizing the Window 403

 

Chapter 32: Core Data Relationships 407

Edit the Model 407

Create Custom NSManagedObject Classes 409

Lay Out the Interface 411

EmployeeView.xib 413

Events and nextResponder 414

 

Chapter 33: Core Animation 417

Scattered 417

Implicit Animation and Actions 423

Challenge 1 425

Challenge 2 425

 

Chapter 34: Concurrency 427

Multithreading 427

Improving Scattered: Time Profiling in Instruments 431

NSOperationQueue 435

For the More Curious: Faster Scattered 438

Challenge 439

 

Chapter 35: Cocoa and OpenGL 441

A Simple Cocoa/OpenGL Application 442

 

Chapter 36: NSTask 451

ZIPspector 451

Challenge: .tar and .tgz files 460

 

Chapter 37: Distributing Your App 461

Build Configurations 461

Creating a Release Build 464

Application Sandboxing 466

The Mac App Store 468

 

Chapter 38: The End 471

 

Index 473

What People are Saying About This

“Aaron’s book is the gold standard for Mac OS X programming books—beautifully written, and thoughtfully sculpted. The best book on Leopard development.”
—Scott Stevenson, www.theocacao.com

“This is the first book I’d recommend for anyone wanting to learn Cocoa from scratch. Aaron’s one of the few (perhaps only) full-time professional Cocoa instructors, and his teaching experience shows in the book.”
—Tim Burks, software developer and creator of the Nu programming language

“If you’re a UNIX or Windows developer who picked up a Mac OS X machine recently in hopes of developing new apps or porting your apps to Mac users, this book should be strongly considered as one of your essential reference and training tomes.”
—Kevin H. Spencer, Apple Certified Technical Coordinator

Preface

If you are developing applications for the Mac, or are hoping to do so, this book is just the resource you need. Does it cover everything you will ever want to know about programming for the Mac? Of course it doesn’t. But it does cover probably 80% of what you need to know. You can find the remaining 20%, the 20% that is unique to you, in Apple’s online documentation.

This book, then, acts as a foundation. It covers the Objective-C language and the major design patterns of Cocoa. It will also get you started with the three most commonly used developer tools: Xcode, Interface Builder, and Instruments. After reading this book, you will be able to understand and utilize Apple’s online documentation.

There is a lot of code in this book. Through that code, I will introduce you to the idioms of the Cocoa community. My hope is that by presenting exemplary code, I can help you to become not just a Cocoa developer, but a stylish Cocoa developer.

This third edition includes technologies introduced in Mac OS X 10.4 and 10.5. These include Xcode 3, Objective-C 2, Core Data, the garbage collector, and CoreAnimation.

This book is written for programmers who already know some C programming and something about objects. You are not expected to have any experience with Mac programming. It’s a hands-on book and assumes that you have access to Mac OS X and the developer tools. The developer tools are free. If you bought a shrink-wrapped copy of Mac OS X, the installer for the developer tools was on the DVD. The tools can also be downloaded from the Apple Developer Connection Web site (http://developer.apple.com/).

I have tried to make this book as useful for you as possible, if not indispensable. That said, I’d love to hear from you if you have any suggestions for improving it.

Aaron Hillegass
aaron@bignerdranch.com

From the B&N Reads Blog

Customer Reviews