Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Integer Array to Data [duplicate]

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()

//
///////
like image 398
ericg Avatar asked Jun 27 '17 23:06

ericg


People also ask

How do you find duplicate numbers in an integer array?

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.

Can array store duplicate values?

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.

How do you remove duplicates from an integer array?

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.


1 Answers

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.

like image 137
OOPer Avatar answered Oct 13 '22 01:10

OOPer