Objective-C 指针和 swift [英] Objective-C pointer and swift

查看:69
本文介绍了Objective-C 指针和 swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 apple 文档,可惜例子都是用objective-c写的,但是我对Swift语言有信心,有些东西的意思看不懂,特别是,在这个例子中:

I'm following an apple document, but unfortunately the examples are written on objective-c, but I have confidence with Swift language and can not understand the meaning of some things, in particular, in this example:

void RunLoopSourcesPerformRoutine (void *info){
    RunLoopSource*  obj = (RunLoopSource*)info;
    [obj sourceFired];
}

这一行:RunLoopSource* obj = (RunLoopSource*)info;

参数:void *info表示info是指向void的指针,那么我可以放任何类型数据结构的地址,下面各种苹果文档我看到这个:void *info 翻译成 swift 语言是:

the parameter: void *info indicates that info is a pointer to void, then I can put the address of any type of data structure, following various apple documents I saw that the translation of this : void *info into swift language is :

info: UnsafeMutableRawPointer?

现在,RunLoopSource* obj = (RunLoopSource*)info; 行表明 obj 是一个类型为 RunLoopSource 的变量,并为此分配了 (RunLoopSource *)info,但确切地说,这句话是什么意思?:(RunLoopSource *) info,以及它如何翻译成 swift 语言?

Now, the RunLoopSource* obj = (RunLoopSource*)info; line indicates that obj is a variable of type: RunLoopSource, and to this is assigned the value of (RunLoopSource *) info, but precisely What does it mean this statement? : (RunLoopSource *) info, and how it translates in swift language ?

推荐答案

你正在处理的 (void *info) 是一个 C pointer-to-void,它以 UnsafeRawPointer 的形式出现在 Swift 中.这意味着类型信息已被丢弃,内存在其他地方进行管理.

What you are dealing with (void *info) is a C pointer-to-void, which arrives into Swift as a form of UnsafeRawPointer. This means that type info has been cast away and that memory is being managed elsewhere.

为了按照您认为的那样使用这个东西,即一个 RunLoopSource,您需要将它明确地描述为一个 RunLoopSource.在 C 中,您将进行转换,如您发布的示例代码:(RunLoopSource*)info.在 Swift 中,您重新绑定.

In order to work with this thing as what you believe it to be, i.e. a RunLoopSource, you need to characterize it explicitly as a RunLoopSource. In C, you would cast, as in the example code you posted: (RunLoopSource*)info. In Swift, you rebind.

请注意,在您的情况下,由于此 UnsafeMutableRawPointer 已被包装在 Optional 中,因此整个事情变得稍微复杂一些,并且必须先解包才能执行任何操作.

Observe that in your case this whole thing has been made just a little more complicated by the fact that this UnsafeMutableRawPointer has been wrapped in an Optional, and will have to be unwrapped before you can do anything at all.

假设,那么,就您而言,info 确实是一个 UnsafeMutableRawPointer? 绑定到 RunLoopSource,您可以说:

Assuming, then, in your case, that info is really an UnsafeMutableRawPointer? bound to a RunLoopSource, you can say:

let rlsptr = info!.assumingMemoryBound(to: RunLoopSource.self)
let rls = rlsptr.pointee

现在rls 一个RunLoopSource,你可以随心所欲地使用它.但是请记住,内存是不受管理的,因此您应该只在此时此地使用它.

Now rls is a RunLoopSource and you can work with it however you like. Keep in mind, however, that the memory is unmanaged, so you should work with it only here and now.

编辑 顺便说一句,Apple 有一份关于这整个问题的非常好的文档:https://swift.org/migration-guide/se-0107-migrate.html

EDIT By the way, Apple has a really nice document on this entire matter: https://swift.org/migration-guide/se-0107-migrate.html

这篇关于Objective-C 指针和 swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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