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

后端 未结 6 613
南笙
南笙 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:38

    1. When you add a swift file in your Objective-C project, Xcode will prompt to add Objective-C bridging header file, so allow the header file to be created.
    2. In your Objective-C implementation file where you want to access the TestViewController property foobar. Use the following import syntax and replace the ProjectName with your project.

    #import "ProjectName-Swift.h"

    Objective-C implementation file:

    #import "ViewController.h"
    #import "ProjectName-Swift.h"
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        TestViewController *testViewController = [[TestViewController alloc] init]; // success
        BOOL prop = testViewController.foobar;
        NSLog(@"Property: %d", prop);
    }
    
    @end
    

    For more details go through the Apple Documents

提交回复
热议问题