斯威夫特数组元素的地址 [英] Swift array element address
问题描述
这是一个实际的问题,我打破它变成的,比如说树干发送文件,1000字节
This is a practical question, i'm sending a file by breaking it into trunks of, 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)
}
一切是伟大的,直到我要插入一个序列号为dataPackage,在位置0,所以很自然,我会改变上面
Everything was great, until I want to insert a sequence number into dataPackage, at position 0, so naturally, I would change the above to
data.getBytes(&dataPackage[1], NSRange(location: offset, length: 999))
原来只有1个单一的元素被复制到dataPackage。其余的999元素被复制到不神秘其中
It turned out that only 1 single element is copied to dataPackage. The rest 999 elements were copied to dont-know-where
我的问题是,1)如何完成这项工作,和2)如何在迅速阵列被编址,以这样的方式与放大器;数据由[i] =&放大器;数据+ I(如图在第一实施例)但和放大器;数据[I + K] =&安培;!数据[I] + K
My question is, 1) how to make this work, and 2) how array in swift is addressed, in such a way that &data[i] = &data + i (as shown in the 1st example) but &data[i+k] != &data[i] + k
编辑:我解决了(1)通过执行
I solved (1) by doing
data.getBytes(&dataPackage + 1, NSRange(location: offset, length: 999))
问题(2)保持
推荐答案
不要的getBytes(安培; dataPackage [I] + K,..)
,我想,今年5月会导致访问冲突
Do not getBytes(&dataPackage[i] + k, ..)
, I think, this may causes "access violation"
考虑这个code:
struct MyStruct {
var _val = [Int](count: 1000, repeatedValue: 0);
subscript(idx:Int) -> Int {
get {
println("get: [\(idx)] -> val(\(_val[idx]))")
return _val[idx]
}
set(val) {
println("set: [\(idx)] old(\(_val[idx])) -> new(\(val))")
_val[idx] = val
}
}
}
func myFunc(ptr:UnsafeMutablePointer<Int>, val:Int) {
println("mutating: ptr(\(ptr)) <- \(val)")
ptr.memory = val
}
MYSTRUCT
就位于阵列
的包装。
myFunc的
收到一个可变的指针和变异它的价值。
myFunc
receives a mutable pointer and mutates the value of it.
通过这一点,当哟做的:
With this, when yo do:
var foo = MyStruct()
myFunc(&foo[1], 12)
输出:
get: [1] -> val(0)
mutating: ptr(0x00007fff561619d8) <- 12
set: [1] old(0) -> new(12)
请参阅?
- 复制的值
地方
- 变异的值
地方
- 回写从
地方
- Copy the value to
somewhere
- Mutate the value of
somewhere
- Write-back from
somewhere
我不知道在哪里地方
是,虽然。
I don't know where somewhere
is, though.
当你做这样的:
var foo = MyStruct()
myFunc(&foo[1] + 1, 12)
输出:
get: [1] -> val(0)
set: [1] old(0) -> new(0)
mutating: ptr(0x00007fff5c0b79e0) <- 12
这一次,我的猜测是:
- 复制的值
地方
- 让
损坏
为某处+ 1
- 回写与未突变值从
- 放弃
地方
- 变异的值
损坏
- Copy the value to
somewhere
- let
corrupted
assomewhere + 1
- Write-back with unmutated value from
somewhere
- Discard
somewhere
- Mutate the value of
corrupted
在另一方面,当你做:
var foo = [Int](count:1000, repeatedValue:0)
myFunc(&foo + 1, 12)
这意味着:
- 让
PTR
作为第一个元素的指针富
- 让
PTR
为PTR + 1
- 变异的值
PTR
- let
ptr
as the pointer of the first element offoo
- let
ptr
asptr + 1
- Mutate the value of
ptr
因此, foo的[1]
将 12
成功像你一样。
As a result, foo[1]
will be 12
successfully as you did.
其实在出(&安培;
)阵列
的很特别为<一提到href=\"https://developer.apple.com/library/ios/documentation/swift/conceptual/buildingcocoaapps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-XID_16\"相对=nofollow>文档。
Actually in-out(&
) of Array
is very special as mentioned in the document.
当一个函数声明为服用UnsafeMutablePointer参数,它可以接受任何如下:
When a function is declared as taking an UnsafeMutablePointer argument, it can accept any of the following:
- 为零,这是一个空指针 传递
- 一个UnsafeMutablePointer值
- 一个进出的前pression,其操作数的类型类型,它是作为左值 的转交地址的左值存储
- 一个在出[类型]的值,这是作为一个指针数组的开始经过,和寿命扩展为呼叫的持续时间
- nil, which is passed as a null pointer
- An UnsafeMutablePointer value
- An in-out expression whose operand is a stored lvalue of type Type, which is passed as the address of the lvalue
- An in-out [Type] value, which is passed as a pointer to the start of the array, and lifetime-extended for the duration of the call
只有内置的阵列
的最后一项功能。
Only the built-in Array
has the last feature.
所以,当你通过&放大器; notAnArrayLvalue
(包括&放大器;阵列[我]
)以 UnsafeMutablePointer&LT;类型&GT;
参数,长度应始终 1
,你不应该 +
或 -
是
So, when you pass ¬AnArrayLvalue
(including &array[i]
) to UnsafeMutablePointer<Type>
parameter, the length should be always 1
, and you should not +
or -
it.
这篇关于斯威夫特数组元素的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!