Structure
Data
iOS 8.0+
iPadOS 8.0+
Mac Catalyst 8.0+
macOS 10.10+
tvOS 9.0+
watchOS 2.0+
A byte buffer in memory.
@frozen struct Data
Overview
The Data value type allows simple byte buffers to take on the behavior of Foundation objects. You can create empty or pre-populated buffers from a variety of sources and later add or remove bytes. You can filter and sort the content, or compare against other buffers. You can manipulate subranges of bytes and iterate over some or all of them.
Data bridges to the NSData class and its mutable subclass, NSMutableData. You can use these interchangeably in code that interacts with Objective-C APIs.
Topics
Structures
struct Base64DecodingOptionsstruct Base64EncodingOptionsstruct ReadingOptionsOptions to control the reading of data from a URL.struct SearchOptionsOptions that control a data search operation.struct WritingOptionsOptions to control the writing of data to a URL.
Initializers
init()Creates an empty data buffer.init(_:)init(base64Encoded:options:)Initialize aDatafrom a Base-64, UTF-8 encodedData.init(buffer:)Creates a data buffer with copied memory content using a buffer pointer.init(bytes:)init(bytes: UnsafeRawPointer, count: Int)Creates data with copied memory content.init(bytesNoCopy: UnsafeMutableRawPointer, count: Int, deallocator: Data.Deallocator)Creates a data buffer with memory content without copying the bytes.init(capacity: Int)Creates an empty data buffer of a specified size.init(contentsOf: URL, options: Data.ReadingOptions) throwsCreates data by reading from the specified URL.init(count: Int)Creates a new data buffer with the specified count of zeroed bytes.init(repeating: UInt8, count: Int)Initialize aDatawith a repeating byte pattern
Instance Properties
var bytes: RawSpanvar count: IntThe number of bytes in the data.var endIndex: Data.IndexThe endIndexinto the data.var indices: Range<Int>var mutableBytes: MutableRawSpanvar mutableSpan: MutableSpan<UInt8>var span: Span<UInt8>var startIndex: Data.IndexThe beginning index into the data.
Instance Methods
func advanced(by: Int) -> DataReturns a new data buffer created by removing the given number of bytes from the front of the original data.func append(_:)func append(UnsafePointer<UInt8>, count: Int)Appends the specified bytes from memory to the end of the data.func append(contentsOf:)Appends the bytes in the specified sequence to the end of the data.func base64EncodedData(options: Data.Base64EncodingOptions) -> DataReturns Base-64 encoded data.func base64EncodedString(options: Data.Base64EncodingOptions) -> StringReturns a Base-64 encoded string.func copyBytes(to: UnsafeMutablePointer<UInt8>, count: Int)Copies the contents of the data to memory.func copyBytes(to:from:)Copies a subset of the contents of the data to memory.func enumerateBytes((UnsafeBufferPointer<UInt8>, Data.Index, inout Bool) -> Void)Enumerates the contents of the data’s buffer.func index(after: Data.Index) -> Data.IndexReturns the index that immediately follows the specified index.func index(before: Data.Index) -> Data.IndexReturns the index that immediately precedes the specified index.func insert(UInt8, at: Data.Index)func range(of: Data, options: Data.SearchOptions, in: Range<Data.Index>?) -> Range<Data.Index>?Find the givenDatain the content of thisData.func remove(at: Data.Index) -> UInt8func removeAll(keepingCapacity: Bool)func removeSubrange(Range<Data.Index>)func repalceSubrangeFast(_:with:)Replaces a region of bytes in the data with new bytes from a collection.func replaceSubrange(Range<Data.Index>, with: UnsafeRawPointer, count: Int)Replaces a region of bytes in the data with bytes from memory.func reserveCapacity(Int)Prepares the collection to store the specified number of elements, when doing so is appropriate for the underlying type.func resetBytes(in: Range<Data.Index>)Sets a region of the data buffer to 0.func subdata(in: Range<Data.Index>) -> DataReturns a new copy of the data in a specified range.func withContiguousStorageIfAvailable<E, ResultType>((UnsafeBufferPointer<UInt8>) throws(E) -> ResultType) throws(E) -> ResultType?func withUnsafeBytes(_:)func withUnsafeMutableBytes(_:)func write(to: URL, options: Data.WritingOptions) throwsWrites the contents of the data buffer to a location.
Subscripts
subscript(_:)Accesses the bytes at the specified range of indexes.
Type Aliases
typealias IndexA type used to indicate a position in a data’s buffer.typealias IndicesA type used to indicate a range of positions in a data’s buffer.
Enumerations
enum DeallocatorA deallocator you use to customize how the backing store is deallocated for data created with the no-copy initializer.
Default Implementations
Relationships
Conforms To
ContiguousBytesDataProtocolMultipartKit.MultipartPartConvertibleMutableDataProtocolSwift.BidirectionalCollectionSwift.CollectionSwift.CopyableSwift.CustomDebugStringConvertibleSwift.CustomReflectableSwift.CustomStringConvertibleSwift.DecodableSwift.EncodableSwift.EquatableSwift.EscapableSwift.HashableSwift.MutableCollectionSwift.RandomAccessCollectionSwift.RangeReplaceableCollectionSwift.SendableSwift.SendableMetatypeSwift.Sequence