码迷,mamicode.com
首页 > 其他好文 > 详细

Calling Functions With Pointer Parameters

时间:2019-03-31 09:49:24      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:com   可变   元素   creat   var   access   pre   必须   ring   

技术图片?

参数类型是Constant Pointer

也就是 UnsafePointer<Type>

可以传入的类型:

  1. UnsafePointer<Type>/UnsafeMutablePointer<Type>/AutoreleasingUnsafeMutablePointer<Type>
  2. String。如果TypeUInt8Int8
  3. 可变类型的 Typein-out 类型。
  4. [Type] 类型,被当作指向第一个元素的地址

例子如下:

func takesAPointer(_ p: UnsafePointer<Float>) {
    // ...
}

var x: Float = 0.0
takesAPointer(&x)
takesAPointer([1.0, 2.0, 3.0])

The pointer you pass to the function is only guaranteed to be valid for the duration of the function call. Do not persist the pointer and access it after the function has returned.

参数类型是 Mutable Pointer

UnsafeMutablePointer<Type>
可以传入的类型:

  1. UnsafeMutablePointer<Type>
  2. 可变类型的 Typein-out 类型。
  3. [Type] 类型,必须是可变类型。

例子:

func takesAMutablePointer(_ p: UnsafeMutablePointer<Float>) {
    // ...
}

var x: Float = 0.0
//是var类型
var a: [Float] = [1.0, 2.0, 3.0]
takesAMutablePointer(&x)
takesAMutablePointer(&a)

参数类型是Autoreleasing Pointer

AutoreleasingUnsafeMutablePointer<Type>
可以传入:

  1. AutoreleasingUnsafeMutablePointer
  2. 可变类型的 Typein-out 类型。

参数类型是 Function Pointer

可以传入的类型有:

  1. top-level Swift function
  2. a closure literal
  3. a closure declared with the @convention(c) attribute
  4. nil

例子:

func customCopyDescription(_ p: UnsafeRawPointer?) -> Unmanaged<CFString>? {
    // return an Unmanaged<CFString>? value
}

 var callbacks = CFArrayCallBacks(
    version: 0,
    retain: nil,
    release: nil,
    copyDescription: customCopyDescription,
    equal: { (p1, p2) -> DarwinBoolean in
        // return Bool value
    }
)
var mutableArray = CFArrayCreateMutable(nil, 0, &callbacks)

Calling Functions With Pointer Parameters

标签:com   可变   元素   creat   var   access   pre   必须   ring   

原文地址:https://www.cnblogs.com/huahuahu/p/Calling-Functions-With-Pointer-Parameters.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!