Unable to access Swift 4 class from Objective-C: “Property not found on object of type”

后端 未结 6 614
南笙
南笙 2020-12-04 18:55

Using the latest Xcode 9 beta, I\'m seemingly completely unable to access properties on Swift classes. Even odder, I can access the class itself to instantiate it or whateve

6条回答
  •  星月不相逢
    2020-12-04 19:44

    The rules for exposing Swift code to Objective-C have changed in Swift 4. Try this instead:

    @objc var foobar = true
    

    As an optimization, @objc inference have been reduced in Swift 4. For instance, a property within an NSObject-derived class, such as your TestViewController, will no longer infer @objc by default (as it did in Swift 3).

    Alternatively, you could also expose all members to Objective-C at once using @objcMembers:

    @objcMembers class TestViewController: UIViewController {
        ...
    }
    

    This new design is fully detailed in the corresponding Swift Evolution proposal.

提交回复
热议问题