Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access Swift var from Objective-C View controller - iOS

I have an app with both Objective-C and Swift based view controllers. I am programatically opening a Swift based view controller from one of my Objective-C based view controllers. The problem I am having is that I can't access the Swift variables from my Objective-C code.

My Swift code:

@IBOutlet weak var profPicture: UIImageView!
@IBOutlet weak var profVerified: UIImageView!
@IBOutlet weak var profName: UILabel!
var passedUser:PFUser!

My Objective-C code:

MyProfileViewController *screen = [[MyProfileViewController alloc] initWithNibName:@"Main" bundle:nil];
self.dataPass = screen;
dataPass.profName.text = @"";
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:screen animated:YES completion:nil];

I am able to access @ properties like IBOutlets, but I can't access and edit the var objects.

I am doing this, because I want to pass data into the Swift based view controller from the Objective-C based view controller when I am presenting the Swift based view controller.

Why can't I access the var objects?

Here is the error I am getting:

enter image description here

The error is as follows:

Property 'passedUser' not found on object of type 'MyProfileViewController *'

Thanks for your time, Dan.

like image 207
Supertecnoboff Avatar asked Nov 02 '15 13:11

Supertecnoboff


People also ask

How do I add Objective-C to Swift?

Add any Objective-C file to your Swift project by choosing File -> New -> New File -> Objective-C File. Upon saving, Xcode will ask if you want to add a bridging header. Choose 'Yes'.


1 Answers

Swift 4:

Use @objc keyword to access the swift variable.

class Car: NSObject {
    @objc let name: String
    @objc let numberOfWheels: Int
    let manufacturer: String
}
like image 52
Kishore Kumar Avatar answered Oct 01 '22 12:10

Kishore Kumar