简体   繁体   English

从 Objective-C ZA2F2ED4F8EBC2CBB4C21A29DC40AB61Z 调用 Swift function

[英]Calling a Swift function from Objective-C class

I'm trying to call a Swift function from an Objective-C class and it's throwing:我正在尝试从 Objective-C ZA2F2ED4F8EBC2CBB14C21A29DZ0 和它调用 Swift function 和

No visible @interface for 'TUtils' declares the selector 'run'

TUtils.swift TUtils.swift

@objc
public class TUtils: NSObject  {
    
    @objc func run() {
        print("Hello test")
    }
}

Consume.m消费.m

#import <RKit/RKit-Swift.h>
    
inline int tempFun () {
    TUtils *obj = [TUtils new];
    [obj run];
    return 0;
}

thanks谢谢

Typical issue is that functions need to be public in order to be visible by Objective-C.典型的问题是功能需要public才能被 Objective-C 看到。 As docs say : 正如文档所说

By default, the generated header contains interfaces for Swift declarations marked with the public or open modifier.默认情况下,生成的 header 包含标记为 public 或 open 修饰符的 Swift 声明的接口。 If your app target has an Objective-C bridging header, the generated header also includes interfaces marked with the internal modifier.如果您的应用目标有一个 Objective-C 桥接 header,则生成的 header 还包括标有 internal 修饰符的接口。

So try to change your function to所以尝试将您的 function 更改为

@objc public func run() {

and see if this helps.看看这是否有帮助。 If it does not, clear and rebuild from scratch.如果没有,请清除并从头开始重建。 You can also open RKit-Swift.h (by command+click on it) and check how TUtils was declared and which functions Objective-C sees.您还可以打开RKit-Swift.h (通过命令+单击它)并检查TUtils的声明方式以及 Objective-C 看到的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM