Create an Array in Swift from an NSData Object - ios

I'm trying to store an array of integers to disk in swift. I can get them into an NSData object to store, but getting them back out into an array is difficult. I can get a raw COpaquePointer to the data with data.bytes but can't find a way to initialize a new swift array with that pointer. Does anyone know how to do it?
import Foundation
var arr : UInt32[] = [32,4,123,4,5,2];
let data = NSData(bytes: arr, length: arr.count * sizeof(UInt32))
println(data) //data looks good in the inspector
// now get it back into an array?

You can use the getBytes method of NSData:
// the number of elements:
let count = data.length / sizeof(UInt32)
// create array of appropriate length:
var array = [UInt32](count: count, repeatedValue: 0)
// copy bytes into array
data.getBytes(&array, length:count * sizeof(UInt32))
print(array)
// Output: [32, 4, 123, 4, 5, 2]
Update for Swift 3 (Xcode 8): Swift 3 has a new type struct Data
which is a wrapper for NS(Mutable)Data with proper value semantics.
The accessor methods are slightly different.
Array to Data:
var arr: [UInt32] = [32, 4, UInt32.max]
let data = Data(buffer: UnsafeBufferPointer(start: &arr, count: arr.count))
print(data) // <20000000 04000000 ffffffff>
Data to Array:
let arr2 = data.withUnsafeBytes {
Array(UnsafeBufferPointer<UInt32>(start: $0, count: data.count/MemoryLayout<UInt32>.stride))
}
print(arr2) // [32, 4, 4294967295]
Update for Swift 5:
Array to Data:
let arr: [UInt32] = [32, 4, UInt32.max]
let data = Data(buffer: UnsafeBufferPointer(start: arr, count: arr.count))
print(data) // <20000000 04000000 ffffffff>
Data to Array:
var arr2 = Array<UInt32>(repeating: 0, count: data.count/MemoryLayout<UInt32>.stride)
_ = arr2.withUnsafeMutableBytes { data.copyBytes(to: $0) }
print(arr2) // [32, 4, 4294967295]

It's also possible to do this using an UnsafeBufferPointer, which is essentially an "array pointer", as it implements the Sequence protocol:
let data = NSData(/* ... */)
// Have to cast the pointer to the right size
let pointer = UnsafePointer<UInt32>(data.bytes)
let count = data.length / 4
// Get our buffer pointer and make an array out of it
let buffer = UnsafeBufferPointer<UInt32>(start:pointer, count:count)
let array = [UInt32](buffer)
This eliminates the need for initializing an empty array with duplicated elements first, to then overwrite it, although I have no idea if it's any faster. As it uses the Sequence protocol this implies iteration rather than fast memory copy, though I don't know if it's optimized when passed a buffer pointer. Then again, I'm not sure how fast the "create an empty array with X identical elements" initializer is either.

Here is a generic way to do it.
import Foundation
extension Data {
func elements <T> () -> [T] {
return withUnsafeBytes {
Array(UnsafeBufferPointer<T>(start: $0, count: count/MemoryLayout<T>.size))
}
}
}
let array = [1, 2, 3]
let data = Data(buffer: UnsafeBufferPointer(start: array, count: array.count))
let array2: [Int] = data.elements()
array == array2
// IN THE PLAYGROUND, THIS SHOWS AS TRUE
You must specify the type in the array2 line. Otherwise, the compiler cannot guess.

If you are dealing with Data to Array (I know for sure my array is going to be [String]), I am quite happy with this:
NSKeyedUnarchiver.unarchiveObject(with: yourData)
I hope it helps

Related

how to store values in a 1D array into a 2D array in Swift 4

Hi I would like to store values of a 1D array into a 2D array.
My 1D array has 50 elements and I want to store it in a 5x10 array, but whenever I do that, it always gives me a "Index out of range" error
Any help would be appreciated thanks!
var info2d = [[String]]()
var dataArray = outputdata.components(separatedBy: ";")
for j in 0...10 {
for i in 0...5 {
info2d[i][j] = dataArray[(j)*5+i]
print(info2d[i][j])
}
}
Lots of error in your code.
info2d must be initialised with default values before using it by index
// initialising 2d array with empty string value
var info2d = [[String]](repeating: [String](repeating: "", count: 10), count: 5)
Secondly for loop with ... includes the last value too, use ..<
for j in 0..<10 {
//...
}
Thirdly (j)*5+i is incorrect too.
Better Read how to use arrays, collections and for loop in swift.
https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html
https://docs.swift.org/swift-book/LanguageGuide/CollectionTypes.html
I would make use of ArraySlice for this.
var arr2D = [[String]]()
for i in 0..<5 {
let start = i * 10
let end = start + 10
let slice = dataArray[start..<end] //Create an ArraySlice
arr2D.append(Array(slice)) //Create new Array from ArraySlice
}

Swift Array memory address changes when referring to the same variable

I was learning Python and wanted to confirm a certain behavior in Objective-C and Swift.
The test was as follows:
Python
def replace(list):
list[0] = 3
print(list)
aList = [1, 2, 3]
print(aList)
replace(aList)
print(aList)
Objective-C
- (void)replace:(NSMutableArray *)array {
array[0] = #1;
NSLog(#"array: %#, address: %p\n%lx", array, array, (long)&array);
}
NSMutableArray *array = [#[#1, #2, #3] mutableCopy];
NSLog(#"original: %#, address: %p \n%lx", array, array, (long)&array);
[self replace:array];
NSLog(#"modified: %#, address: %p \n%lx", array, array, (long)&array);
Swift
var numbers = [1, 2, 3]
let replace = { (var array:[Int]) -> Void in
array[0] = 2
print("array: \(array) address:\(unsafeAddressOf(array as! AnyObject))")
}
print("original: \(numbers) address:\(unsafeAddressOf(numbers as! AnyObject))")
replace(numbers)
print("modified: \(numbers) address:\(unsafeAddressOf(numbers as! AnyObject))")
All the results came out as expected except for the address part in Swift. In Objective-C, the address of array remained the same in original and modified, but the print result of Swift was:
original: [1, 2, 3] address:0x00007f8ce1e092c0
array: [2, 2, 3] address:0x00007f8ce1f0c5d0
modified: [1, 2, 3] address:0x00007f8ce4800a10
Is there something I'm missing?
Arrays in Swift have value semantics, not the reference semantics of arrays in Python and Objective-C. The reason you're seeing different addresses (and addresses at all) is that every time you do an as! AnyObject cast, you're actually telling Swift to bridge your Array<Int> struct to an instance of NSArray. Since you bridge three times, you get three different addresses.
You shouldn't need to think about the address of a Swift array, but if you'd like to (momentarily) get the address of an array's buffer, you can do it this way:
func getBufferAddress<T>(array: [T]) -> String {
return array.withUnsafeBufferPointer { buffer in
return String(reflecting: buffer.baseAddress)
}
}
This lets you see the buffer's copy-on-write in action:
var numbers = [1, 2, 3]
let numbersCopy = numbers
// the two arrays share a buffer here
print(getBufferAddress(numbers)) // "0x00007fba6ad16770"
print(getBufferAddress(numbersCopy)) // "0x00007fba6ad16770"
// mutating `numbers` causes a copy of its contents to a new buffer
numbers[0] = 4
// now `numbers` has a new buffer address, while `numbersCopy` is unaffected
print(getBufferAddress(numbers)) // "0x00007ff23a52cc30"
print(getBufferAddress(numbersCopy)) // "0x00007fba6ad16770"

NSData from UInt8

I have recently found a source code in swift and I am trying to get it to objective-C. The one thing I was unable to understand is this:
var theData:UInt8!
theData = 3;
NSData(bytes: [theData] as [UInt8], length: 1)
Can anybody help me with the Obj-C equivalent?
Just to give you some context, I need to send UInt8 to a CoreBluetooth peripheral (CBPeripheral) as UInt8. Float or integer won't work because the data type would be too big.
If you write the Swift code slightly simpler as
var theData : UInt8 = 3
let data = NSData(bytes: &theData, length: 1)
then it is relatively straight-forward to translate that to Objective-C:
uint8_t theData = 3;
NSData *data = [NSData dataWithBytes:&theData length:1];
For multiple bytes you would use an array
var theData : [UInt8] = [ 3, 4, 5 ]
let data = NSData(bytes: &theData, length: theData.count)
which translates to Objective-C as
uint8_t theData[] = { 3, 4, 5 };
NSData *data = [NSData dataWithBytes:&theData length:sizeof(theData)];
(and you can omit the address-of operator in the last statement,
see for example How come an array's address is equal to its value in C?).
In Swift 3
var myValue: UInt8 = 3 // This can't be let properties
let value = Data(bytes: &myValue, count: MemoryLayout<UInt8>.size)
In Swift,
Data has a native init method.
// Foundation -> Data
/// Creates a new instance of a collection containing the elements of a
/// sequence.
///
/// - Parameter elements: The sequence of elements for the new collection.
/// `elements` must be finite.
#inlinable public init<S>(_ elements: S) where S : Sequence, S.Element == UInt8
#available(swift 4.2)
#available(swift, deprecated: 5, message: "use `init(_:)` instead")
public init<S>(bytes elements: S) where S : Sequence, S.Element == UInt8
So, the following will work.
let values: [UInt8] = [1, 2, 3, 4]
let data = Data(values)

Data structure for bidimensional array

I have an array of string arrays.
it has to be declared empty, and it's later filled one array at a time.
I have tried any possible data structure, but none of them seems to be working.
For example, if I declare
var array = [[String]]()
the app crashes when I try to append elements, like
array[1][0] = "Some string"
The only way I managed to make it work is by declaring the array with a fixed number of elements, but that's not good for my purpose.
I thought this was very easy to accomplish, but I encountered a lot of problems, any solution?
You cannot append to empty array like this array[1][0] = "Some string". If you want to do that, you need to create an array with certain size and repeated value. But you want to create 2d-array, so that can be implemented like this:
extension Array {
static func bidimensional<T>(row: Int, _ column: Int, repeatedValue: T) -> [[T]] {
var resultArray = [[T]]()
for _ in 0...column {
resultArray.append([T](count: row, repeatedValue: repeatedValue))
}
return resultArray
}
}
var arr = [String].bidimensional(2, 2, repeatedValue: "")
arr[1][0] = "Hello there!"
print(arr[1][0]) // "Hello there!"
Update
Extension add new functionality to an existing class, structure, or enumeration type. In our case we extend Array type with function bidimensional. The static means, that it's type method, not instance method. The T means, that it's generic method. In this case, you can call your bidimensional function not only with one specific type (like String), but any type you want (String, Int, Double, etc.). Well, the bidimensional's func code is pretty simple. We just create empty 2D resultArray with our custom (T) type. Then we fill this array with our repeatedValue and return it. Thats all.
P.S. To be clear with generics there are several examples:
[Int].bidimensional(2, 2, repeatedValue: 0) // 2x2 with repeatedValue 0
[Double].bidimensional(5, 1, repeatedValue: 2.1) // 5x1 with repeatedValue 2.1
------
struct MyCustomStruct {
var variable: String
}
var myCustomStruct = MyCustomStruct(variable: "repeat")
var arr = [MyCustomStruct].bidimensional(2, 2, repeatedValue: myCustomStruct)
print(arr[0][1].variable) // "repeat"
Update 2 for this comment.
class MyClass {
var arr = [[String]]()
func fill2DArrayInLoop() {
arr = [String].bidimensional(2, 2, repeatedValue: "")
for i in 0...1 {
for j in 0...1 {
arr[i][j] = "row: \(i); col: \(j)"
}
}
}
}
let myClass = MyClass()
myClass.fill2DArrayInLoop()
print(myClass.arr[0][1]) // "row: 0, col: 1"
Found the way to do this better (see this answer):
class MyClass {
var arr = Array(count: 2, repeatedValue: Array(count: 2, repeatedValue: ""))
func fill2DArrayInLoop() {
for i in 0...1 {
for j in 0...1 {
arr[i][j] = "row: \(i); col: \(j)"
}
}
}
}
let myClass = MyClass()
myClass.fill2DArrayInLoop()
print(myClass.arr[0][1]) // "row: 0, col: 1"
Now you don't need to create extension. Just put another declaration of array to repeatedValue: parameter.

Convert NSData bytes to custom object

I am starting a project to create an iOS app to communicate with a device over BLE. Being a new effort, I am trying to do this is Swift if possible. The interface uses GATT and an existing set of custom message structures. I get to a point where I have the data from BLE in an NSData object. I'd like to cast it or directly convert it to my message structure in a fairly generic way.
I know that I can extract the data by hand either directly from the byte array from the NSData object or using an NSInputStream. While that works, it could be a maintenance issue and the interface has a number of different messages in it.
Is there an easier ways to do this?
I'd be willing to create the message structures in Objective-C and do the casting there, but my knowledge of Objective-C is not much better than my knowledge of Swift.
Some sample code of what I've been playing in my playground is shown below. It all works as expected.
func getBytesFromNSData(data: NSData, start: Int) -> [UInt8] {
let count = data.length / sizeof(UInt8)
let remaining = count - start
let range = NSMakeRange(start, remaining )
var dataArray = [UInt8](count: remaining, repeatedValue: 0)
data.getBytes(&dataArray, range: range)
return dataArray
}
class TestObject {
var a: Byte
var b: Byte
init() {
a = 0x01
b = 0x02
}
init(data: NSData) {
let dataBytes = getBytesFromNSData(data, 0)
a = Byte(dataBytes[0])
b = Byte(dataBytes[1])
}
func populateFromStream(data: NSData) {
var stream = NSInputStream(data: data)
stream.open()
var bytesRead = stream.read(&a, maxLength: 1)
println("\(bytesRead)")
bytesRead = stream.read(&b, maxLength: 1)
println("\(bytesRead)")
}
func toArray() -> [Byte] {
var result = [Byte](count: 2, repeatedValue: 0)
result[0] = a
result[1] = b
return result
}
}
let test = TestObject()
let testArray = test.toArray()
let length = testArray.count
let testData = NSData(bytes: testArray, length: length)
println("\(testData)")
let testIn = [ Byte(0x0d), Byte(0x0e) ]
let testDataIn = NSData(bytes: testIn, length: testIn.count)
println("\(testDataIn)")
let testConstructor = TestObject(data: testDataIn)
var testObject = TestObject()
testObject.populateFromStream(testDataIn)
I found a method that is fairly generic that may work is some cases.
Create an Objective-C riding header
Create the data structure as an Objective-C structure
Import the header with the data structure into the bridging header
Assuming that you have a struct called Foo and an NSData object called rawData:
Use the following code to get an cast a pointer.
let structureSize = sizeof(Foo)
var myObject = UnsafeMutablePointer<Foo>.alloc(1).memory
rawData.getbytes(&myObject, length: structureSize)
This will not work in all instances and unfortunately does work in my particular case. The specific problems I have found are:
The Objective-C structure is word aligned. If your structure is not properly aligned to work boundaries, you may have a size that is incorrect. (something I ran into in my particular interface)
If you and dealing with a system that doesn't send the data in the same order you are expecting, this will not handle any byte order conversion, that would still need to be done and the structure would possibly need to be reordered to compensate. That work might negate any saving from this method.
This is the most concise method I have found if it happens to work with your particular message formats.

Resources