斯威夫特数组元素的地址 [英] Swift array element address

查看:167
本文介绍了斯威夫特数组元素的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个实际的问题,我打破它变成的,比如说树干发送文件,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)

请参阅?


  1. 复制的值地方

  2. 变异的值地方

  3. 回写地方

  1. Copy the value to somewhere
  2. Mutate the value of somewhere
  3. 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. 复制的值地方

  2. 损坏某处+ 1

  3. 回写未突变值从 的地方

  4. 放弃地方

  5. 变异的值损坏

  1. Copy the value to somewhere
  2. let corrupted as somewhere + 1
  3. Write-back with unmutated value from somewhere
  4. Discard somewhere
  5. Mutate the value of corrupted

在另一方面,当你做:

var foo = [Int](count:1000, repeatedValue:0)
myFunc(&foo + 1, 12)

这意味着:


  1. PTR 作为第一个元素的指针

  2. PTR PTR + 1

  3. 变异的值 PTR

  1. let ptr as the pointer of the first element of foo
  2. let ptr as ptr + 1
  3. 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 &notAnArrayLvalue(including &array[i]) to UnsafeMutablePointer<Type> parameter, the length should be always 1, and you should not + or - it.

这篇关于斯威夫特数组元素的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆