swift - &(内存中的地址)是 Swift 中的运算符还是我使用的是 C?

标签 swift ampersand

因此,我正在研究 Ales Tsurko 对 Chris Adamson 的 CoreAudio 示例的出色 [并且需要] 快速转换。但是,我遇到了一些我似乎无法找到直接答案的问题。

在例子中有这样的东西:

    var theErr = noErr
    var dictionarySize: UInt32 = 0
    var isWritable: UInt32 = 0
    theErr = AudioFileGetPropertyInfo(
            audiofile, 
            kAudioFilePropertyInfoDictionary, 
            &dictionarySize, 
            &isWritable
    )

我很好奇的是“&”(与号)运算符的使用。

现在在普通的 C 系列语言中,this 指的是变量的内存地址,在这里似乎就是这样做的。

令我困惑的是我在第四行写的是 Swift 还是 C 代码?

我知道您混合使用了 Obj-C 和 Swift,但我在 Swift 文档中找不到任何关于将 & 作为内存运算符使用的内容。那么这种使用是否没有记录,或者我是否已经暂时从 Swift 跳出到 C/Obj-C 中?

谢谢

最佳答案

来自 the Apple documentation on Swift :

When a function is declared as taking an UnsafeMutablePointer argument, it can accept the same operands as UnsafeMutablePointer for any type Type.

If you have declared a function like this one:

func takesAMutableVoidPointer(x: UnsafeMutablePointer<Void>)  {
    // ...
}

You can call it in any of the following ways:

var x: Float = 0.0, y: Int = 0
var p: UnsafeMutablePointer<Float> = nil, q: UnsafeMutablePointer<Int> = nil
var a: [Float] = [1.0, 2.0, 3.0], b: [Int] = [1, 2, 3]
takesAMutableVoidPointer(nil)
takesAMutableVoidPointer(p)
takesAMutableVoidPointer(q)
takesAMutableVoidPointer(&x)
takesAMutableVoidPointer(&y)
takesAMutableVoidPointer(&a)
takesAMutableVoidPointer(&b)

所以这是将对象的地址传递给函数,并且是原生的 Swift。

关于swift - &(内存中的地址)是 Swift 中的运算符还是我使用的是 C?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35991492/

相关文章:

ios - Swift - TableView 未填充数据但未显示任何错误

javascript - WebStorm/usr/local/bin/node --debug-brk=55834 --nolazy app.js

c++ - 带有未命名参数的私有(private)复制构造函数?

php - 我需要在 PHP 5.5.X 及更高版本中使用 & 符号吗?

ios - Swift 中的 PFQueryTableViewController 使用 Cloud Code 函数

ios - 删除以编程方式添加的约束

ios - 有没有等同于 Swift/Obj-C/iOS 的 ruby​​ 控制台?

ios7 - 在 Xcode 6 下运行 iOS7 项目 - 图像 Assets 不显示

css - 在 Sass SCSS 中的嵌套选择器中结合使用选择器列表和 &

sass - SCSS : How to use two classes together with an ampersand?