Skip to Content
Cocoa and Objective-C: Up and Running
book

Cocoa and Objective-C: Up and Running

by Scott Stevenson
April 2010
Beginner to intermediate
416 pages
10h 16m
English
O'Reilly Media, Inc.
Content preview from Cocoa and Objective-C: Up and Running

Chapter 6. More Objective-C

Now that all of the basic building blocks are in place, I can show you some of the more technical details of how Objective-C works. I’m going to fine-tune your code, talk more about reference counting, and then look at some unique features.

Most of the sections in this chapter describe techniques that you will use on a regular basis. A few of the topics are more advanced and you may not use them often, but if you need them, you really need them. All of them help you understand how Cocoa works.

More on Memory Management

In Chapter 5, I walked you through Objective-C’s traditional memory management system, called reference counting. Each object has a retain count, which is ostensibly the number of other things that are currently using it. The good news is that you don’t need to constantly check the retain count. You need to keep track of only your own references; Cocoa will do the rest.

Caution

You can call the retainCount method on any object, but don’t read too much into what it returns. It may not always reflect the exact number of retains and releases you have issued. Cocoa intervenes in certain cases.

The retain count starts at 1 when you create an object with alloc. It goes up and down as you call retain, release, and autorelease. When the retain count reaches 0, the Objective-C runtime will call dealloc on the object so that it can be removed from memory (see Figure 6-1).

Figure 6-1. When the retain count reaches 0, the dealloc method is called and the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Cocoa Programming for Mac OS X, Third Edition

Cocoa Programming for Mac OS X, Third Edition

Aaron Hillegass
Learning Cocoa with Objective-C, 3rd Edition

Learning Cocoa with Objective-C, 3rd Edition

Paris Buttfield-Addison, Jonathon Manning
Learning Cocoa with Objective-C, 4th Edition

Learning Cocoa with Objective-C, 4th Edition

Paris Buttfield-Addison, Jonathon Manning, Tim Nugent

Publisher Resources

ISBN: 9780596809812Errata Page