You should first extend IntegerType to extract its bytes in NSData form using getBytes and use sizeof () of dynamicType to determine the length of the byte:
extension IntegerType {
var data: NSData {
var source = self
return NSData(bytes: &source, length: sizeof(self.dynamicType))
}
}
With this done you should create a method to concatenate your data before sending:
func getDataToSend(first: Int8,_ second: UInt16,_ third: Int) -> NSData {
let mutableData = NSMutableData()
mutableData.appendData(first.data)
mutableData.appendData(second.data)
mutableData.appendData(third.data)
return mutableData
}
And you can also extend NSData to make it easier to extract your elements from it:
extension NSData {
var getElements:(first: Int8, second: UInt16, third: Int) {
var resultInt8: Int8 = 0
var resultUInt16: UInt16 = 0
var resultInt: Int = 0
getBytes(&resultInt8, range: NSRange(location: 0, length: sizeof(Int8)))
getBytes(&resultUInt16, range: NSRange(location: 1, length: sizeof(UInt16)))
getBytes(&resultInt, range: NSRange(location: 3, length: sizeof(Int)))
return (resultInt8, resultUInt16, resultInt)
}
}
Testing:
let a0: Int8 = 3
let a1: UInt16 = 231
let a2: Int = 1024
let dataToSend = getDataToSend(a0, a1, a2)
print(dataToSend.length) // 11
let elementsFromData = dataToSend.getElements // (.0 3, .1 231, .2 1024)
let firstElement = elementsFromData.first // 3
let secondElement = elementsFromData.second // 231
let thirdElement = elementsFromData.third // 1024