How can I convert an integer Array to Data?
This is what I have, but I am getting lost in figuring out the final step. I am only interested in the Swift 3 solution.
import Foundation
var buffer = [UInt64]( repeating: 0, count: 1000 )
for x in 0 ..< 1000
{
buffer[x] = UInt64(x)
}
///////
// What goes here to place buffer into myData
var myData = Data()
//
///////
One of the most common ways to find duplicates is by using the brute force method, which compares each element of the array to every other element. This solution has the time complexity of O(n^2) and only exists for academic purposes.
We know that HashSet doesn't allow duplicate values in it. We can make use of this property to check for duplicates in an array. The idea is to insert all array elements into a HashSet . Now the array contains a duplicate if the array's length is not equal to the set's size.
We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.
When you want a Data
, better check the initializers of Data
.
Data
init<SourceType>(buffer: UnsafeBufferPointer<SourceType>)
seems to be useful in your case, as UnsafeBufferPointer
can easily made from Array
.
var myData = buffer.withUnsafeBufferPointer {Data(buffer: $0)}
print(myData as NSData)
//-> <00000000 00000000 01000000 00000000 02000000 00000000 03000000 00000000 ...
If the result is not what you expect, you need to explain it more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With