Calling Functions With Pointer Parameters

?

参数类型是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)

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

时间: 2024-10-25 23:19:56

Calling Functions With Pointer Parameters的相关文章

Swift functions with external parameters

详细定义 Function Parameter Names Function parameters have both an external parameter name and a local parameter name. An external parameter name is used to label arguments passed to a function call. A local parameter name is used in the implementation o

Swift 函数的定义与调用(Defining and Calling Functions)

当你定义一个函数时,你可以定义一个或多个有名字和类型的值,作为函数的输入(称为参数,parameters),也可以定义某种类型的值作为函数执行结束的输出(称为返回类型). 每个函数有个函数名,用来描述函数执行的任务.要使用一个函数时,你用函数名"调用",并传给它匹配的输入值(称作实参,arguments).一个函数的实参必须与函数参数表里参数的顺序一致. 在下面例子中的函数叫做"greetingForPerson",之所以叫这个名字是因为这个函数用一个人的名字当做输

Swift - 函数(Functions)总结 - 比较 与 C# 的异同

1.0 函数的定义与调用( Defining and Calling Functions ) 习惯了C#了语法,看到下面的这样定义输入参数实在感到非常别扭,func 有点 Javascript的感觉,还算习惯.函数调用与其他语言没什么区别 //有输入参数和返回值的函数 //输入参数为名name,数据类型为String //返回值 String类型 func SayHello(name:String) ->String { return "Hello,"+name; } //调用函

Delphi DLL制作和加载 Static, Dynamic, Delayed 以及 Shared-Memory Manager

一 Dll的制作一般分为以下几步:1 在一个DLL工程里写一个过程或函数2 写一个Exports关键字,在其下写过程的名称.不用写参数和调用后缀.二 参数传递1 参数类型最好与window C++的参数类型一致.不要用DELPHI的数据类型.2 最好有返回值[即使是一个过程],来报出调用成功或失败,或状态.成功或失败的返回值最好为1[成功]或0[失败].一句话,与windows c++兼容.3 用stdcall声明后缀.4 最好大小写敏感.5 无须用far调用后缀,那只是为了与windows 1

(C/C++) Callback Function

原文: http://www.codeguru.com/cpp/cpp/cpp_mfc/callbacks/article.php/c10557/Callback-Functions-Tutorial.htm Callback Functions Tutorial Introduction If you are reading this article, you probably wonder what callback functions are. This article explains

iOS -- warnings

Semantic Warnings Warning Message -WCFString-literal input conversion stopped due to an input byte that does not belong to the input codeset UTF-8 -WNSObject-attribute         __attribute ((NSObject)) may be put on a typedef only, attribute is ignore

IOS 警告 收集

Semantic Warnings Warning Message -WCFString-literal input conversion stopped due to an input byte that does not belong to the input codeset UTF-8 -WNSObject-attribute __attribute ((NSObject)) may be put on a typedef only, attribute is ignored -Wabst

转载一篇debug文章

http://versprite.com/og/ios-reverse-engineering-part-two-debugging-and-tracing-with-lldb/ iOS Reverse Engineering Part Two: Debugging and Tracing with LLDB Overview In our previous post – http://versprite.com/og/ios-reverse-engineering-part-one-confi

斯坦福大学操作系统

Pintos-斯坦福大学操作系统Project详解-Project1 前言:  本实验来自斯坦福大学cs140课程,只限于教学用途,以下是他们对于Pintos系统的介绍:  Pintos is a simple operating system framework for the 80x86 architecture. It supports kernel threads, loading and running user programs, and a file system, but it