This is a practical question, I am sending a file breaking it into chests, say 1000 bytes
data = NSData.dataWithContentsOfFile(path)
var dataPackage : [Byte](count: 1000, repeatedValue: 0)
for offset = 0; offset < data.length; {
// omit some range check here
data.getBytes(&dataPackage, NSRange(location: offset, length: 1000))
send(dataPackage)
}
Everything was wonderful until I wanted to insert the serial number in the dataPackage, to position 0, so, naturally, I would change the above to
data.getBytes(&dataPackage[1], NSRange(location: offset, length: 999))
It turned out that only one single item is copied to dataPackage. The remaining 999 items were copied to d-know-where
My question is: 1) how to do this, and 2) how the array is solved in swift, so that the data & data [i] = & data + i (as shown in the first example) but & data [i + k]! = & data [i] + k
Edit: I solved (1) by doing
data.getBytes(&dataPackage + 1, NSRange(location: offset, length: 999))
Question (2) remains
source
share