Accessing Objective-c base class's instance variables from a Swift class

Great query. We have tried to hard to get this done. The only working solution I found

get value by using self.valueForKey("aVariable_")

set value using self.setValue("New Value", forKey: "aVariable_")

Hope that helps. Possible solution without altering super class.


I couldn't find a "proper" way to do this, but I needed badly for it to work. My solution was to create a simple getter method in my Objective C superclass, like this:

header file

@interface ObjcClass : NSObject {
    NSString *myVariable;
}
- (NSString *)myVariable;


in the implementation file

- (NSString *)myVariable {
    return myVariable;
}

I'd love to hear of a better way of doing it, but this at least works.


I've searched a lot for this. Eventually I changed my code from:

@interface PrjRec : NSObject {
    @public
    NSString* name;
}
@end

To:

@interface PrjRec : NSObject {
}

@property NSString* name;

@end

similar to @JasonTyler solution. Then I can access to my object property from Swift code with simple dot notation <object instance>.name,

But I needed to change all existing objective-c references from

<object instance>->name

To:

<object instance>.name

or

_name

if inside class unit.

I hope for a better solution too.