Objc代码找不到swift中定义的Bool变量

har*_*hoo 15 objective-c swift

我在一个Swift文件中定义了var,但在另一个Objective-C文件中,当我尝试设置此var时,编译器抱怨它无法找到var.我该如何解决这个问题?这是代码:在swift中:

var isCreating: Bool!
Run Code Online (Sandbox Code Playgroud)

在objc:

SelectMemberViewController *ctrl = [[SelectMemberViewController alloc]init];
ctrl.isCreating = YES
Run Code Online (Sandbox Code Playgroud)

然后编译器抱怨:在'SelectMemberViewController'类型的对象上找不到属性'isCreating'

mat*_*att 28

问题是Objective-C世界中没有任何东西对应于a Bool!.因此,此声明不会暴露给Objective-C.Bool如果您希望Objective-C能够看到它,则需要将其声明为plain .

  • var isCreating:Bool = false.最初的第一次是修复它. (9认同)