Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

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

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 whatever, but completely unable to access properties on it.

So if I have this Swift class:

import UIKit

class TestViewController: UIViewController {
    var foobar = true
}

And I try to do this:

TestViewController *testViewController = [[TestViewController alloc] init]; // success
testViewController.foobar; // error

What exactly am I doing wrong? New project with Xcode 9.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...