NSData and its mutable subclass
NSMutable provide data objects, object-oriented wrappers for byte buffers. Data objects let simple allocated buffers (that is, data with no embedded pointers) take on the behavior of Foundation objects.
- iOS 2.0+
- macOS 10.0+
- tvOS 9.0+
- watchOS 2.0+
NSData creates static data objects, and
NSMutable creates dynamic data objects.
NSMutable are typically used for data storage and are also useful in Distributed Objects applications, where data contained in data objects can be copied or moved between applications.
The size of the data is subject to a theoretical limit of about 8 ExaBytes (in practice, the limit should not be a factor).
NSData class and its subclasses provide methods to quickly and easily save their contents to disk. To minimize the risk of data loss, these methods provide the option of saving the data atomically. Atomic writes guarantee that the data is either saved in its entirety, or it fails completely. The atomic write begins by writing the data to a temporary file. If this write succeeds, then the method moves the temporary file to its final location.
While atomic write operations minimize the risk of data loss due to corrupt or partially-written files, they may not be appropriate when writing to a temporary directory, the user’s home directory or other publicly accessible directories. Any time you work with a publicly accessible file, you should treat that file as an untrusted and potentially dangerous resource. An attacker may compromise or corrupt these files. The attacker can also replace the files with hard or symbolic links, causing your write operations to overwrite or corrupt other system resources.
Avoid using the
write(to: method (and the related methods) when working inside a publicly accessible directory. Instead initialize an
File object with an existing file descriptor and use the
NSFile methods to securely write the file.