使用最新的 Xcode 9 beta,我似乎完全无法访问 Swift 类的属性。更奇怪的是,我可以访问类本身来实例化它或其他任何东西,但完全无法访问它的属性。
所以如果我有这个 Swift 类:
import UIKit
class TestViewController: UIViewController {
var foobar = true
}
我试着这样做:
TestViewController *testViewController = [[TestViewController alloc] init]; // success
testViewController.foobar; // error
我究竟做错了什么?使用 Xcode 9 的新项目。
最佳答案
将 Swift 代码暴露给 Objective-C 的规则在 Swift 4 中发生了变化。试试这个:
@objc var foobar = true
作为优化,@objc
推断在 Swift 4 中已经减少。例如,NSObject
派生类中的属性,例如您的 TestViewController
,将不再默认推断@objc
(就像在 Swift 3 中所做的那样)。
或者,您也可以使用 @objcMembers
立即将所有 成员公开给 Objective-C:
@objcMembers class TestViewController: UIViewController {
...
}
这个新设计在 the corresponding Swift Evolution proposal 中有详细说明。 .
关于ios - 无法从 Objective-C : "Property not found on object of type" 访问 Swift 4 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56083558/