Search code examples
iosobjective-cswiftsegueobjc-bridging-header

Segue from Obj-c to swift can't find property


i'm attempting to passing an int from objective-c file to swift.

The steps that I have taken do indeed segue, however sadly it does not pass the int across due to the following error: Property 'productKey' not found on object of type 'ProductViewController *'

Prepare Segue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if ([segue.identifier isEqualToString:@"viewProduct"]) {
    ProductViewController *destVC = segue.destinationViewController;
    destVC.productKey = brandID;
  }
}

Perform Segue

dispatch_async(dispatch_get_main_queue(), ^(){
   [self performSegueWithIdentifier:@"viewProduct" sender:self];
});

Swift File

class ProductViewController: UIViewController {

    var productKey: Int!

    override func viewDidLoad() {
        super.viewDidLoad()
           print(productKey)
    }
}

Header Bridge

 //
//  Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "ARSceneViewController.h"

Solution

  • If you are using Swift file in objc classes Bridging header won't help here.

    Xcode will create Header file which you need to import

    First goto build settings and search for Defines Modules and set it to true.

    Now in .m file add following header file

    #import "YourTargetName-Swift.h" 
    

    and build the project

    and also add @objc before your property

    Hope it is helpful