You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add a new set of initializers to Data to aid efficiency and clarity when wrapping NSData.
addresses:
rdar://problem/26385078 Data() really needs a init(length:) and replaceBytes(in:withBytes:)
rdar://problem/26508250 Add more specific init method to Data to indicate keeping a ref type
/// Initialize a `Data` with the contents of an Array.
140
155
///
141
156
/// - parameter bytes: An array of bytes to copy.
@@ -156,15 +171,33 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
156
171
157
172
/// Initialize a `Data` with the specified size.
158
173
///
174
+
/// This initializer doesn not necessarily allocate the requested memory right away. Mutable data allocates additional memory as needed, so `capacity` simply establishes the initial capacity. When it does allocate the initial memory, though, it allocates the specified amount.
175
+
///
176
+
/// This method sets the `count` of the data to 0.
177
+
///
178
+
/// If the capacity specified in `capacity` is greater than four memory pages in size, this may round the amount of requested memory up to the nearest full page.
179
+
///
159
180
/// - parameter capacity: The size of the data.
160
181
publicinit?(capacity:Int){
161
182
iflet d =NSMutableData(capacity: capacity){
162
-
_wrapped =_SwiftNSData(immutableObject: d)
183
+
_wrapped =_SwiftNSData(mutableObject: d)
163
184
}else{
164
185
returnnil
165
186
}
166
187
}
167
188
189
+
/// Initialize a `Data` with the specified count of zeroed bytes.
190
+
///
191
+
/// - parameter count: The number of bytes the data initially contains.
/// Initialize a `Data` by adopting a reference type.
254
+
///
255
+
/// You can use this initializer to create a `struct Data` that wraps a `class NSData`. `struct Data` will use the `class NSData` for all operations. Other initializers (including casting using `as Data`) may choose to hold a reference or not, based on a what is the most efficient representation.
256
+
///
257
+
/// If the resulting value is mutated, then `Data` will invoke the `mutableCopy()` function on the reference to copy the contents. You may customize the behavior of that function if you wish to return a specialized mutable subclass.
258
+
///
259
+
/// - parameter reference: The instance of `NSData` that you wish to wrap. This instance will be copied by `struct Data`.
/// Initialize a `Data` by adopting a mutable reference type.
265
+
///
266
+
/// You can use this initializer to create a `struct Data` that wraps a `class NSMutableData`. `struct Data` will use the `class NSMutableData` for all operations. Other initializers (including casting using `as Data`) may choose to hold a reference or not, based on a what is the most efficient representation.
267
+
///
268
+
/// If the resulting value is mutated, then `Data` will invoke the `mutableCopy()` function on the reference to copy the contents. You may customize the behavior of that function if you wish to return a specialized mutable subclass.
269
+
///
270
+
/// - warning: For performance reasons, this method does not copy the reference on initialization. It assumes that the reference is uniquely held by the struct. If you continue to mutate the reference after invoking this initializer, you may invalidate the copy-on-write and value semantics of `struct Data`. It is recommended that you do not keep the reference after initializing a `Data` with it.
271
+
/// - parameter mutableReference: The instance of `NSMutableData` that you wish to wrap.
0 commit comments