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

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!

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

Just clean & build project.


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.


Swift 4:

Use @objc keyword to access the swift variable.

class Car: NSObject {
    @objc let name: String
    @objc let numberOfWheels: Int
    let manufacturer: String
}