Calling Functions With Pointer Parameters
Posted huahuahu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Calling Functions With Pointer Parameters相关的知识,希望对你有一定的参考价值。
?
参数类型是Constant Pointer
也就是 UnsafePointer<Type>
可以传入的类型:
UnsafePointer<Type>
/UnsafeMutablePointer<Type>
/AutoreleasingUnsafeMutablePointer<Type>
String
。如果Type
是UInt8
或Int8
。- 可变类型的
Type
的in-out
类型。 [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>
可以传入的类型:
UnsafeMutablePointer<Type>
- 可变类型的
Type
的in-out
类型。 [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>
可以传入:
- AutoreleasingUnsafeMutablePointer
- 可变类型的
Type
的in-out
类型。
参数类型是 Function Pointer
可以传入的类型有:
- top-level Swift function
- a closure literal
- a closure declared with the @convention(c) attribute
- 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的主要内容,如果未能解决你的问题,请参考以下文章
C++Calling Functions: A Tutorial - Klaus Iglberger - CppCon 2020 - 知识点目录
C++Calling Functions: A Tutorial - Klaus Iglberger - CppCon 2020 - 知识点目录
C++Calling Functions: A Tutorial - Klaus Iglberger - CppCon 2020 - 知识点目录
2-4. Using auto with Functions
銆怟ata Daily 2銆慍alculating with Functions
[Ramda] Convert Object Methods into Composable Functions with Ramda