Skip to Content
Objective-C
book

Objective-C

by Jiva DeVoe
February 2011
Intermediate to advanced
400 pages
8h 15m
English
Wiley
Content preview from Objective-C

Chapter 14. Using NSValue, NSNumber, and NSData

In This Chapter

  • Boxing Datatypes for Use in Collections

  • Working with NSNumber, and NSValue

  • Using NSData and NSMutableData

As I highlight in Chapter 13, when working with collections, collections are capable of storing only valid Objective-C objects. Collections are incapable of storing scalars, structures, or other arbitrary low-level data. This is an inconvenience, but one that the designers of the Foundation framework anticipated and solved.

In order to store scalars and structures in collections, you need to use a class wrapper for these values. In other words, a class that enables you to store the value inside of an object. The Foundation framework provides three primary classes for this purpose, NSValue, NSNumber, and NSData.

The NSValue class is the simplest of these classes, providing a low-level interface for the arbitrary storage of virtually any C datatype. For example, you can store structures within it, you can store ranges within it, and so on. Once the data is stored within the NSValue instance, you can then use the instance of NSValue in a collection object. Because NSValue is relatively low level, it does not provide some of the conveniences of higher-level abstractions. Its purpose is to be flexible but it is limited in its capabilities because it can only store simple stack-allocated data. NSNumber is a subclass of NSValue. It provides a higher level abstraction to the NSValue data encoding system specifically for the purposes ...

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

Beginning Objective-C

Beginning Objective-C

James Dovey, Ash Furrow
More iPhone Development with Objective-C

More iPhone Development with Objective-C

David Mark, Jayant Varma, Jeff LaMarche, Alex Horovitz, Kevin Kim

Publisher Resources

ISBN: 9780470479223Purchase book