Cannot access Swift var from Objective-C View controller - iOS
Asked Answered
F

5

21

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.

Floriculture answered 2/11, 2015 at 13:56 Comment(6)
On which line are you getting an error? What is the error message?Rheotaxis
@Rheotaxis I updated my question.Floriculture
You did not declare them public.Houri
@HermannKlecker Tried that now, and it still comes up with the same error.Floriculture
Look at this answer. According to that, if I understood it right, initializing the property (with nil?) and making them optional might help #26366582Houri
@Rheotaxis Please repost your original answer, it the end it DID work, I was just having some Xcode cache issues. So I cleaned Xcode and my project and tried your answer again and it worked!Floriculture
R
11

You will have to declare the variable as public.

By default, the variables have internal access, that means they can be accessed only from inside their module. Obj-C code is not in their module.

Rheotaxis answered 2/11, 2015 at 14:1 Comment(0)
R
25

Swift 4:

Use @objc keyword to access the swift variable.

class Car: NSObject {
    @objc let name: String
    @objc let numberOfWheels: Int
    let manufacturer: String
}
Retainer answered 25/4, 2018 at 6:54 Comment(0)
R
11

You will have to declare the variable as public.

By default, the variables have internal access, that means they can be accessed only from inside their module. Obj-C code is not in their module.

Rheotaxis answered 2/11, 2015 at 14:1 Comment(0)
C
8

I had the same issue with an ObjectiveC enum, that was a variable in a Swift class and wasn't accessible in ObjectiveC.

Declaring the variable as public didn't work, instead I had to give the enum a default value when declaring it, for it to be included in the [project]-Swift.h header.

 var firstQuestion: AnswerResult = AnswerNone

The following didn't work:

var firstQuestion: AnswerResult!
Cartan answered 22/8, 2016 at 14:25 Comment(0)
H
1

With these all answers, sometimes compiler doesn't generate appropriate obj-c code for our swift code. In that case,

Just clean & build project.

Holo answered 15/10, 2018 at 8:58 Comment(0)
T
-1

You need to use @objc public for the variable to expose it to Objective-C:

@IBOutlet @objc public weak var profName: UILabel!
Timeout answered 3/2, 2021 at 4:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.