objective-c - 如何在 Objective-C 中使用带有 inout 参数的 Swift 中的方法?

标签 objective-c swift

我要

func foo(inout stop: Bool) -> Void {
    // ...
}

在我的 Objective-C 部分使用。但它永远不会在 Module-Swift.h header 中生成。如果我用@objc 标记它,

Method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C

发生错误。

最佳答案

您不能使用 inout与 Objective-C 桥接时的参数,但如果使用 UnsafeMutablePointer<T> 也可以做类似的事情( T 在您的情况下将是 Bool )。它看起来像这样:

@objc func foo(stop: UnsafeMutablePointer<Bool>) -> Void {
    if stop != nil {
        // Use the .pointee property to get or set the actual value stop points to
        stop.pointee = true
    }
}

例子

测试类.swift:

public class TestClass: NSObject {
    @objc func foo(stop: UnsafeMutablePointer<Bool>) -> Void {
        stop.pointee = true
    }
}

Objective-C :

TestClass *test = [[TestClass alloc] init];
BOOL stop = false;
[test foo:&stop];
// stop is YES here

关于objective-c - 如何在 Objective-C 中使用带有 inout 参数的 Swift 中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26287921/

相关文章:

objective-c - 如何检测何时在 NSTableView 中选择了一行或多行?

objective-c - 在本地和远程之间切换 iOS 应用程序中使用的 url

objective-c - 在 NSTableView 行中显示多个对象

ios - 我想设计一个屏幕,根据特定条件在同一屏幕上显示不同数量的视频

ios - 使用UIBezierPath在2点之间绘制波浪

ios - UIPopoverPresentationControllerDelegate 协议(protocol)的 2 个 adaptivePresentationStyle 方法之间的区别

iphone - BOX2D 静态体摩擦

objective-c - Obj-C 文字 : Getting "Array subscript is not an integer" on an NSDictionary query

ios - 停止 TableView/TableViewCell 动画(可能在 willdisplaycell 上)

ios - 创建符合另一个协议(protocol)的协议(protocol)变量