5

I'm trying to pass data from an objective-c ViewController to a Swift ViewController and I'm getting this error in my Objective-C controller:

Property 'type' not found on object of type 'UIViewController'

My Objective-C code:

UIViewController *mainController = [self.storyboard instantiateViewControllerWithIdentifier:@"IdentitySelfieViewController"];
mainController.type = "passport";
[self.navigationController pushViewController:mainController animated:YES];

Import into the Bridging-header file:

#import "PassportViewController.h"

And created the corresponding String into my Swift View Controller

let type = ""

Thank you for any help

Naresh
  • 16,698
  • 6
  • 112
  • 113
Isabelle
  • 1,457
  • 5
  • 19
  • 44

2 Answers2

7

The Solution to your problem is like that :

First in your Swift Controller add the @Objc before the class keyword like that :

@objc class ViewController: UIViewController

and that in that controller create a variable like that :

public var myValue:String  = ""

After this in your Objective-C ViewController you need to import this and keep in mind this is very important.

#import "TargetName-Swift.h"

TargetName should be replaced by your target name. After this you can easily call the variables of that Swift Controller like that:

ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"VC"];
vc.myValue = @"Hello";
Shabir jan
  • 2,295
  • 2
  • 23
  • 37
  • You are my hero :) Thank you so much for your time and for your answer. – Isabelle Jun 07 '17 at 09:03
  • So my variable is recognized; it seems I can pass it from Objective-C to Swift. How would you retrieve the value in your Swift Controller? Rather than putting this: public var myValue:String = "" - Thanks again – Isabelle Jun 07 '17 at 09:13
  • its very simple after passing the value you can just the variable in the Swift Controller , it will have that values that you passed. – Shabir jan Jun 07 '17 at 09:38
  • or anyone who may do same dumb mistake as me. In step 2, make sure you use (ProjectName)-Swift.h, NOT (ClassName)-Swift.h from https://stackoverflow.com/questions/24078043/call-swift-function-from-objective-c-class – codeslapper Jul 10 '18 at 14:02
  • 5
    In my case Swift4.2, I need to add @objc before the variable declaration. Example: `@objc public var myValue:String = ""` – Moosa Baloch Dec 11 '18 at 22:59
  • @MoosaBaloch That did the trick for me too, so that's important to remember. – Henrik Hansen Aug 20 '19 at 08:16
  • @objc var foo = "" is not working in swift 5 or xcode 11.2.1 – nishee Dec 10 '19 at 13:38
7

This is your swift class

Step 1:

class SwiftClassController: UIViewController {
//Required variables
@objc var deviceId: String?
@objc var deviceObj: CustomType?

Step 2:

In Objective C class import #import "YourProjectName-Swift.h"

Step 3:

In your Objective C class access variable from Swift file

YourViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"YourVC"];
vc.deviceId = @"1234567";
vc. deviceObj = yourCustomObject;
Naresh
  • 16,698
  • 6
  • 112
  • 113