Data and NSData

Two types of Data?

Steven Curtis
4 min readApr 17, 2020


Photo by Markus Spiske on Unsplash

Difficulty: Beginner | Easy | Normal | Challenging

In Swift Data and NSData are ways to interact with raw binary data.

This article explains what that means, and how we can interact with “Data” in our Swift applications.


  • Coding in Swift Playgrounds (guide HERE)
  • Understand the difference between value and reference types in Swift (guide HERE)
  • Knowledge of mutability in Swift (guide HERE)
  • Understand the principles of copy on write (guide HERE)


Data: Information processed or stored by a computer

Data types: A representation of the type of data that can be processed, for example Integer or String. Described by Apple as a byte buffer in memory

NSData: A static byte buffer that bridges to Data


Both Data and NSData seem quite similar, in that both interact with raw binary data.

The major change between Data and NSData is the name, but some methods are different.

In fact you can access the raw memory contents of the elements that you are working with, using an unsafe pointer to do so. Both the data types described can store raw data more complex than simple Strings, Integers, Booleans, that is things like Images that might be used to write in and out of files, memory and databases. One use of these is the use of Core Data (that uses Data as the Binary Data type); the massive advantage of this is that Core Data does not need to persist every possible type and the developer is responsible for encoding and decoding data while Core Data isn’t overloaded with a different type for every possible type.

There are differences between the types of data that can be Implemented in terms of mutability:

  • Data: Swift-implemented
  • immutable NSData
  • mutable NSMutableData
  • custom subclasses of NSData
  • custom subclasses of NSMutableData