Structure
A byte buffer in memory.
--内存里的字节缓存
@frozen struct Data
--@frozen属性修饰,表明即便在运行时页不能改变Data结构体的结构,例如增删结构体里面的存储属性。
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.
--Data可以
桥接到NSData类及其可变子类NSMutableData。你可以在与Objective-C api交互的代码中交替使用这些类型。
init()
Creates an empty data buffer.
--创建一个空数据缓冲区
init(capacity: Int)
Creates an empty data buffer of a specified size.
--创建指定大小的空数据缓冲区
init(count: Int)
Creates a new data buffer with the specified count of zeroed bytes.
func resetBytes(in: Range
Sets a region of the data buffer to 0
.
init()
Creates an empty data buffer.
init
(S)
Creates a new instance of a collection containing the elements of a sequence.
init
Creates a data buffer with copied memory content using a buffer pointer.
init
Creates a data buffer with copied memory content using a mutable buffer pointer.
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(count: Int)
Creates a new data buffer with the specified count of zeroed bytes.
init(bytes: UnsafeRawPointer, count: Int)
Creates data with copied memory content.
init
Creates a data buffer with copied memory content using a buffer pointer.
init
Creates a data buffer with copied memory content using a mutable buffer pointer.
init(bytesNoCopy: UnsafeMutableRawPointer, count: Int, deallocator: Data.Deallocator)
Creates a data buffer with memory content without copying the bytes.
enum Data.Deallocator
A deallocator you use to customize how the backing store is deallocated for data created with the no-copy initializer.
func write(to: URL, options: Data.WritingOptions)
Writes the contents of the data buffer to a location.
typealias Data.ReadingOptions
Options to control the reading of data from a URL.
typealias Data.WritingOptions
Options to control the writing of data to a URL.
func base64EncodedData(options: Data.Base64EncodingOptions) -> Data
Returns Base-64 encoded data.
func base64EncodedString(options: Data.Base64EncodingOptions) -> String
Returns a Base-64 encoded string.
typealias Data.Base64DecodingOptions
Options to use when decoding data.
typealias Data.Base64EncodingOptions
Options to use when encoding data.
var isEmpty: Bool
A Boolean value indicating whether the collection is empty.
subscript(Range
Accesses the bytes at the specified range of indexes.
subscript(Data.Index) -> UInt8
Accesses the byte at the specified index.
func withUnsafeBytes
Accesses the raw bytes in the data's buffer.
Deprecatedfunc withUnsafeMutableBytes
Mutates the raw bytes in the data's buffer.
Deprecatedfunc copyBytes(to: UnsafeMutablePointer
Copies the contents of the data to memory.
func copyBytes(to: UnsafeMutablePointer
Copies a subset of the contents of the data to memory.
func copyBytes
Copies the bytes in a range from the data into a buffer.
func append(Data)
Appends the specified data to the end of this data.
func append
Append a buffer of bytes to the data.
func append(UnsafePointer
Appends the specified bytes from memory to the end of the data.
func append(contentsOf: [UInt8])
Appends the bytes in the specified array to the end of the data.
func reserveCapacity(Int)
Prepares the collection to store the specified number of elements, when doing so is appropriate for the underlying type.
func remove(at: Int) -> UInt8
Removes and returns the element at the specified position.
func removeAll(keepingCapacity: Bool)
Removes all elements from the collection.
func removeSubrange(Range
Removes the elements in the specified subrange from the collection.
func replaceSubrange(Range
Replaces a region of bytes in the data with new data.
func replaceSubrange
Replaces a region of bytes in the data with new bytes from a collection.
func replaceSubrange
Replaces a region of bytes in the data with new bytes from a buffer.
func replaceSubrange(Range
Replaces a region of bytes in the data with bytes from memory.
func first(where: (UInt8) -> Bool) -> UInt8?
Returns the first element of the sequence that satisfies the given predicate.
func max() -> UInt8?
Returns the maximum element in the sequence.
func max(by: (UInt8, UInt8) -> Bool) -> UInt8?
Returns the maximum element in the sequence, using the given predicate as the comparison between elements.
func min() -> UInt8?
Returns the minimum element in the sequence.
func min(by: (UInt8, UInt8) -> Bool) -> UInt8?
Returns the minimum element in the sequence, using the given predicate as the comparison between elements.
func range(of: Data, options: Data.SearchOptions, in: Range
Finds the range of the specified data as a subsequence of this data, if it exists.
typealias Data.SearchOptions
Options that control a data search operation.
func filter((UInt8) -> Bool) -> Data
Returns a new collection of the same type containing, in order, the elements of the original collection that satisfy the given predicate.
func prefix(Int) -> Data
Returns a subsequence, up to the specified maximum length, containing the initial elements of the collection.
func prefix(through: Int) -> Data
Returns a subsequence from the start of the collection through the specified position.
func prefix(upTo: Int) -> Data
Returns a subsequence from the start of the collection up to, but not including, the specified position.
func prefix(while: (UInt8) -> Bool) -> Data
Returns a subsequence containing the initial elements until predicate
returns false
and skipping the remaining elements.
func suffix(Int) -> Data
Returns a subsequence, up to the given maximum length, containing the final elements of the collection.
func suffix(from: Int) -> Data
Returns a subsequence from the specified position to the end of the collection.
func dropLast(Int) -> Data
Returns a subsequence containing all but the specified number of final elements.
func dropFirst(Int) -> Data
Returns a subsequence containing all but the given number of initial elements.
func drop(while: (UInt8) -> Bool) -> Data
Returns a subsequence by skipping elements while predicate
returns true
and returning the remaining elements.
func advanced(by: Int) -> Data
Returns a new data buffer created by removing the given number of bytes from the front of the original buffer.
func reduce
Returns the result of combining the elements of the sequence using the given closure.
var lazy: LazySequence
A sequence containing the same elements as this sequence, but on which some operations, such as map
and filter
, are implemented lazily.
func forEach((UInt8) -> Void)
Calls the given closure on each element in the sequence in the same order as a for
-in
loop.
func enumerated() -> EnumeratedSequence
Returns a sequence of pairs (n, x), where n represents a consecutive integer starting at zero and x represents an element of the sequence.
func makeIterator() -> Data.Iterator
Returns an iterator over the contents of the data.
struct Data.Iterator
An iterator that operates over the contents of data.
func enumerateBytes((UnsafeBufferPointer
Enumerates the contents of the data's buffer.
Deprecated
func sort(by: (UInt8, UInt8) -> Bool)
Sorts the collection in place, using the given predicate as the comparison between elements.
func sorted() -> [UInt8]
Returns the elements of the sequence, sorted.
func sorted(by: (UInt8, UInt8) -> Bool) -> [UInt8]
Returns the elements of the sequence, sorted using the given predicate as the comparison between elements.
func reversed() -> ReversedCollection
Returns a view presenting the elements of the collection in reverse order.
func subdata(in: Range
Returns a new copy of the data in a specified range.
func split(maxSplits: Int, omittingEmptySubsequences: Bool, whereSeparator: (UInt8) -> Bool) -> [Data]
Returns the longest possible subsequences of the collection, in order, that don’t contain elements satisfying the given predicate.
func split(separator: UInt8, maxSplits: Int, omittingEmptySubsequences: Bool) -> [Data]
Returns the longest possible subsequences of the collection, in order, around elements equal to the given element.
static func == (Data, Data) -> Bool
Returns a Boolean value indicating whether or not two data buffers are equivalent.
func elementsEqual
Returns a Boolean value indicating whether this sequence and another sequence contain the same elements in the same order.
func starts
Returns a Boolean value indicating whether the initial elements of the sequence are the same as the elements in another sequence.
func lexicographicallyPrecedes
Returns a Boolean value indicating whether the sequence precedes another sequence in a lexicographical (dictionary) ordering, using the less-than operator (<
) to compare elements.
func lexicographicallyPrecedes
Returns a Boolean value indicating whether the sequence precedes another sequence in a lexicographical (dictionary) ordering, using the given predicate to compare elements.
typealias Data.Index
A type used to indicate a position in a data's buffer.
var startIndex: Data.Index
The beginning index into the data.
var endIndex: Data.Index
The end index into the data.
func index(Int, offsetBy: Int) -> Int
Returns an index that is the specified distance from the given index.
func index(after: Data.Index) -> Data.Index
Returns the index that immediately follows the specified index.
func index(before: Data.Index) -> Data.Index
Returns the index that immediately precedes the specified index.
typealias Data.Indices
A type used to indicate a range of positions in a data's buffer.
func distance(from: Int, to: Int) -> Int
Returns the distance between two indices.
var description: String
A textual description of the data.
var debugDescription: String
A textual description the data suitable for debugging.
var customMirror: Mirror
A mirror that reflects the data.
var hashValue: Int
The hash value for the data.
class NSData
A static byte buffer that bridges to Data
; use NSData
when you need reference semantics or other Foundation-specific behavior.
class NSMutableData
A dynamic byte buffer that bridges to Data
; use NSMutableData
when you need reference semantics or other Foundation-specific behavior.
typealias Data.ReferenceType
An alias for this value type's equivalent reference type.
typealias Data.Element
typealias Data.Regions
typealias Data.SubSequence
init
(S)
init?(base64Encoded: Data, options: Data.Base64DecodingOptions)
init?(base64Encoded: String, options: Data.Base64DecodingOptions)
init
Deprecated(bytes: S)
init(contentsOf: URL, options: Data.ReadingOptions)
init(from: Decoder)
init(referencing: NSData)
init(repeating: UInt8, count: Int)
var count: Int
var first: UInt8?
The first element of the collection.
var indices: Range
var last: UInt8?
The last element of the collection.
var publisher: Publishers.Sequence
var regions: CollectionOfOne
var underestimatedCount: Int
A value less than or equal to the number of elements in the collection.
func allSatisfy((UInt8) -> Bool) -> Bool
Returns a Boolean value indicating whether every element of a sequence satisfies a given predicate.
func append(UInt8)
Adds an element to the end of the collection.
func append
(contentsOf: S)
func append
(contentsOf: S)
Adds the elements of a sequence or collection to the end of this collection.
func applying(CollectionDifference
Applies the given difference to this collection.
func compactMap
Returns an array containing the non-nil
results of calling the given transformation with each element of this sequence.
func contains(UInt8) -> Bool
Returns a Boolean value indicating whether the sequence contains the given element.
func contains(where: (UInt8) -> Bool) -> Bool
Returns a Boolean value indicating whether the sequence contains an element that satisfies the given predicate.
func copyBytes(to: UnsafeMutableRawBufferPointer) -> Int
func difference
Returns the difference needed to produce this collection’s ordered elements from the given collection.
func difference
Returns the difference needed to produce this collection’s ordered elements from the given collection, using the given predicate as an equivalence test.
func elementsEqual
Returns a Boolean value indicating whether this sequence and another sequence contain equivalent elements in the same order, using the given predicate as the equivalence test.
func encode(to: Encoder)
func firstIndex(of: UInt8) -> Int?
Returns the first index where the specified value appears in the collection.
func firstIndex(where: (UInt8) -> Bool) -> Int?
Returns the first index in which an element of the collection satisfies the given predicate.
func firstRange
func firstRange
func flatMap
Returns an array containing the concatenated results of calling the given transformation with each element of this sequence.
func flatMap
Deprecated
func formIndex(inout Int, offsetBy: Int)
Offsets the given index by the specified distance.
func formIndex(inout Int, offsetBy: Int, limitedBy: Int) -> Bool
Offsets the given index by the specified distance, or so that it equals the given limiting index.
func formIndex(after: inout Int)
Replaces the given index with its successor.
func formIndex(before: inout Int)
Replaces the given index with its predecessor.
func hash(into: inout Hasher)
func index(Int, offsetBy: Int, limitedBy: Int) -> Int?
Returns an index that is the specified distance from the given index, unless that distance is beyond a given limiting index.
func index(of: UInt8) -> Int?
Returns the first index where the specified value appears in the collection.
Deprecatedfunc insert(UInt8, at: Int)
Inserts a new element into the collection at the specified position.
func insert
Inserts the elements of a sequence into the collection at the specified position.
func last(where: (UInt8) -> Bool) -> UInt8?
Returns the last element of the sequence that satisfies the given predicate.
func lastIndex(of: UInt8) -> Int?
Returns the last index where the specified value appears in the collection.
func lastIndex(where: (UInt8) -> Bool) -> Int?
Returns the index of the last element in the collection that matches the given predicate.
func lastRange
func lastRange
func map
Returns an array containing the results of mapping the given closure over the sequence’s elements.
func partition(by: (UInt8) -> Bool) -> Int
Reorders the elements of the collection such that all the elements that match the given predicate are after all the elements that don’t match.
func popFirst() -> UInt8?
Removes and returns the first element of the collection.
func popLast() -> UInt8?
Removes and returns the last element of the collection.
func randomElement() -> UInt8?
Returns a random element of the collection.
func randomElement
Returns a random element of the collection, using the given generator as a source for randomness.
func reduce
Returns the result of combining the elements of the sequence using the given closure.
func removeAll(where: (UInt8) -> Bool)
Removes all the elements that satisfy the given predicate.
func removeFirst() -> UInt8
Removes and returns the first element of the collection.
func removeFirst(Int)
Removes the specified number of elements from the beginning of the collection.
func removeLast() -> UInt8
Removes and returns the last element of the collection.
func removeLast(Int)
Removes the specified number of elements from the end of the collection.
func removeSubrange
Removes the elements in the specified subrange from the collection.
func replaceSubrange
Replaces the specified subrange of elements with the given collection.
func resetBytes
func reverse()
Reverses the elements of the collection in place.
func shuffle()
Shuffles the collection in place.
func shuffle
Shuffles the collection in place, using the given generator as a source for randomness.
func shuffled() -> [UInt8]
Returns the elements of the sequence, shuffled.
func shuffled
Returns the elements of the sequence, shuffled using the given generator as a source for randomness.
func sort()
Sorts the collection in place.
func starts
Returns a Boolean value indicating whether the initial elements of the sequence are equivalent to the elements in another sequence, using the given predicate as the equivalence test.
func swapAt(Int, Int)
Exchanges the values at the specified indices of the collection.
func withContiguousMutableStorageIfAvailable
Call body(p)
, where p
is a pointer to the collection’s mutable contiguous storage. If no such storage exists, it is first created. If the collection does not support an internal representation in a form of mutable contiguous storage, body
is not called and nil
is returned.
func withContiguousStorageIfAvailable
Call body(p)
, where p
is a pointer to the collection’s contiguous storage. If no such storage exists, it is first created. If the collection does not support an internal representation in a form of contiguous storage, body
is not called and nil
is returned.
func withUnsafeBytes
func withUnsafeMutableBytes
subscript(Range
Accesses a contiguous subrange of the collection’s elements.
subscript
subscript((UnboundedRange_) -> ()) -> Data
subscript
Accesses the contiguous subrange of the collection’s elements specified by a range expression.
subscript
static func != (Data, Data) -> Bool
Returns a Boolean value indicating whether two values are not equal.
static func +
Creates a new collection by concatenating the elements of a sequence and a collection.
static func +
Creates a new collection by concatenating the elements of a collection and a sequence.
static func +
Creates a new collection by concatenating the elements of two collections.
static func +=
Appends the elements of a sequence to a range-replaceable collection.
CKRecordValueProtocol
ContiguousBytes
CustomDebugStringConvertible
CustomReflectable
CustomStringConvertible
Decodable
Encodable
Equatable
Hashable
MutableCollection
MutableDataProtocol
RandomAccessCollection
RangeReplaceableCollection
ReferenceConvertible
protocol DataProtocol
protocol MutableDataProtocol
protocol ContiguousBytes