即使我添加了 @objc
,我也无法从 Objective C 中看到将 myVar
设置为 true
的方法/变量和 public
修饰符,并且 setMyVarTrue()
方法的签名中没有 Bool
。
这可能是由于 Swift 的 Bool
和 Objective C 的 BOOL
之间的差异造成的。
其他类方法/变量是可见的,只有这个特定的方法/变量不可见。
如何从 Objective C 设置 Swift 的 Bool
?
Swift 代码:
public class MyViewController : UIViewController {
public var myVar:Bool = false
@objc public func setMyVarTrue() {
self.myVar = true
}
}
objective-c 代码:
MyViewController* myViewController = [MyViewController new];
myViewController.myVar = true // Variable not found
myViewController.setMyVarTrue() // Method not found
[self presentViewController:myViewController animated:NO completion:nil];
最佳答案
你应该这样做
将 @objc 添加到类的声明中
添加一个名为“yourName-swift.h”的文件,其中应包含
@class yourClass;
在 Objective C 文件中导入“yourName-swift.h”,您要在其中调用
你的 Swift 类(class)
关于ios - 在 Objective C 中看不到 Swift 变量/方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52273950/