从Swift类访问Objective-c基类的实例变量。

时间:2023-01-16 12:10:16

Having an Objective c base class:

具有目标c基类:

@interface ObjcClass : NSObject {
    NSString *aVariable_;
}

And a swift sub-class:

和迅速子类:

class SwiftClass : ObjcClass {
    func init() {
        // aVariable_ can't be accessed here. An Objective-c derived
        // class has direct access to it's super's instance variables!
    }
}

How do I access ObjcClass aVariable_ from within SwiftClass?

如何从SwiftClass中访问ObjcClass aVariable_ ?

6 个解决方案

#1


17  

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

伟大的查询。我们已经尽力把这件事办好。我找到的唯一可行的解决方案

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

通过使用self。valueforkey(“aVariable_”)获取值

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

设置使用自我价值。forKey setValue(“新价值”:“aVariable_”)

Hope that helps. Possible solution without altering super class.

希望有帮助。不改变超类的可能解决方案。

#2


5  

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:

我找不到一种“合适”的方式来做这件事,但我非常需要它来工作。我的解决方案是在Objective - C超类中创建一个简单的getter方法,如下所示:

header file

头文件

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


in the implementation file

在实现文件中

- (NSString *)getMyVariable {
    return myVariable;
}

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

我很想知道更好的方法,但至少这是可行的。

#3


3  

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,

类似于@JasonTyler解决方案。然后我可以使用简单的点符号 <对象实例> .name从Swift代码访问我的对象属性,

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

但是我需要修改所有现有的objective-c引用

<object instance>->name

To:

:

<object instance>.name

or

_name

if inside class unit.

如果内部类单位。

I hope for a better solution too.

我也希望有更好的解决办法。

#4


1  

This worked as a pretty neat solution for me, just adding a Swift variable like:

这对我来说是一个非常简洁的解决方案,只需添加一个快速变量,比如:

var myInstanceVar: String {
    return self.value(forKey: "myInstanceVar") as! String
}

#5


0  

If you are willing to have a property, then you can create the property to fit your needs.

如果你想拥有一处房产,那么你可以根据自己的需要来建造它。

@interface ObjcClass : NSObject {
    NSString *aVariable_;
}

@property (nonatomic) NSString *aVariable_;

...

@implementation ObjcClass

@synthesize aVariable_ = aVariable_;

This allows the variable to be accessed as inst->aVariable_ or as inst.aVariable_. In the Objective C class the variable can be accessed as aVariable_ or self.aVariable_.

这允许以inst->aVariable_或inst.aVariable_访问变量。在Objective - C类中,变量可以作为avari_或self.aVariable_访问。

#6


0  

I seriously don't know why anyone does instance variables anymore (for one, they're private by default) vs properties. See Giorgio Calzolato's answer on this (apart from his last line about looking for a better solution - that IS the best solution :) ).

我真的不知道为什么每个人都有实例变量(比如,默认情况下是私有的)vs属性。看看乔治·卡尔佐拉多关于这个问题的答案(除了他关于寻找更好的解决方案的最后一句——这是最好的解决方案)。

In my case I already had a property and was extra perplexed over why it didn't work. But I realized that the property had a custom time and it needed to be added into my SDK-Bridging-Header.h file.

在我的案例中,我已经有了一处房产,而且对它为什么不起作用感到格外困惑。但是我意识到这个属性有一个自定义时间,它需要添加到我的SDK-Bridging-Header中。h文件。

So if your property is set to a custom type like this:

如果你的属性被设置为自定义类型如下:

@property (nonatomic, weak) IBOutlet SDKMyCustomObject *customObject;

...then remember to add it to the bridging header.

…然后记住将它添加到桥接头中。

#1


17  

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

伟大的查询。我们已经尽力把这件事办好。我找到的唯一可行的解决方案

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

通过使用self。valueforkey(“aVariable_”)获取值

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

设置使用自我价值。forKey setValue(“新价值”:“aVariable_”)

Hope that helps. Possible solution without altering super class.

希望有帮助。不改变超类的可能解决方案。

#2


5  

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:

我找不到一种“合适”的方式来做这件事,但我非常需要它来工作。我的解决方案是在Objective - C超类中创建一个简单的getter方法,如下所示:

header file

头文件

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


in the implementation file

在实现文件中

- (NSString *)getMyVariable {
    return myVariable;
}

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

我很想知道更好的方法,但至少这是可行的。

#3


3  

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,

类似于@JasonTyler解决方案。然后我可以使用简单的点符号 <对象实例> .name从Swift代码访问我的对象属性,

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

但是我需要修改所有现有的objective-c引用

<object instance>->name

To:

:

<object instance>.name

or

_name

if inside class unit.

如果内部类单位。

I hope for a better solution too.

我也希望有更好的解决办法。

#4


1  

This worked as a pretty neat solution for me, just adding a Swift variable like:

这对我来说是一个非常简洁的解决方案,只需添加一个快速变量,比如:

var myInstanceVar: String {
    return self.value(forKey: "myInstanceVar") as! String
}

#5


0  

If you are willing to have a property, then you can create the property to fit your needs.

如果你想拥有一处房产,那么你可以根据自己的需要来建造它。

@interface ObjcClass : NSObject {
    NSString *aVariable_;
}

@property (nonatomic) NSString *aVariable_;

...

@implementation ObjcClass

@synthesize aVariable_ = aVariable_;

This allows the variable to be accessed as inst->aVariable_ or as inst.aVariable_. In the Objective C class the variable can be accessed as aVariable_ or self.aVariable_.

这允许以inst->aVariable_或inst.aVariable_访问变量。在Objective - C类中,变量可以作为avari_或self.aVariable_访问。

#6


0  

I seriously don't know why anyone does instance variables anymore (for one, they're private by default) vs properties. See Giorgio Calzolato's answer on this (apart from his last line about looking for a better solution - that IS the best solution :) ).

我真的不知道为什么每个人都有实例变量(比如,默认情况下是私有的)vs属性。看看乔治·卡尔佐拉多关于这个问题的答案(除了他关于寻找更好的解决方案的最后一句——这是最好的解决方案)。

In my case I already had a property and was extra perplexed over why it didn't work. But I realized that the property had a custom time and it needed to be added into my SDK-Bridging-Header.h file.

在我的案例中,我已经有了一处房产,而且对它为什么不起作用感到格外困惑。但是我意识到这个属性有一个自定义时间,它需要添加到我的SDK-Bridging-Header中。h文件。

So if your property is set to a custom type like this:

如果你的属性被设置为自定义类型如下:

@property (nonatomic, weak) IBOutlet SDKMyCustomObject *customObject;

...then remember to add it to the bridging header.

…然后记住将它添加到桥接头中。