ios - 在 Objective C 中看不到 Swift 变量/方法
问题描述
我无法从Objective C中看到要设置的方法/变量,myVar
即使true
我已经添加了@objc
andpublic
修饰符并且方法签名Bool
中没有。setMyVarTrue()
Bool
这可能是由 Swift和 Objective C之间的差异造成的BOOL
。
其他类方法/变量是可见的,只是这个特定的不是。
如何Bool
从 Objective C 设置 Swift?
SWIFT代码:
public class MyViewController : UIViewController {
public var myVar:Bool = false
@objc public func setMyVarTrue() {
self.myVar = true
}
}
目标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;
在要调用的目标 C 文件中导入“yourName-swift.h”
你的快速课程
推荐阅读
- node.js - MongoDB 对象的 View Count 不会增加
- smalltalk - 将 SmallInteger 子类化以表示数字事物?
- ruby-on-rails - 使用嵌入的 Rails 显示带有 ajax 的 html
- arrays - 如何使用 openpyxl 将复杂的 numpy 数组写入 excel
- python - 从不同结构的字典列表创建火花数据框
- ios - CFRunloopRun() 和简单的无限循环之间的区别?
- angularjs - 一个一个地执行解析器?
- indexing - Solr 前缀搜索与罗马数字 - 召回问题
- websocket - 如何修复 Flutter(Dart)WebSocket 上的错误“在收到完整标头之前已关闭连接”
- python - 接收 KeyError:“[Int64Index([ ... dtype='int64', length=1323)] 均不在 [columns] 中”