如何从Swift语言访问Objective-C类方法?

时间:2022-06-01 22:15:17

In my Swift app, I need to access a class method called weibo() as below from Objective-C

在我的Swift应用中,我需要从Objective-C中访问一个名为weibo()的类方法

@interface Weibo : NSObject
+ (Weibo*)weibo;
@end

I have configured the bridging header and tried the following statement

我已经配置了桥接头并尝试了以下语句

let w = Weibo.weibo() as Weibo

It doesn't work.

它不工作。

UPDATE: I have forked the repo and fixed this issue as below.

更新:我已经分叉了repo并修复了如下问题。

let w = Weibo.getWeibo() as Weibo // the method has been changed.

The reason why it didn't work because Swift treats + (Weibo*)weibo; as a convenience constructor. Since weibo is same as the Class name Weibo although the case is different. I need to change the name to getWeibo to fix this issue to support Swift.

它之所以没能成功,是因为斯威夫特的微博;作为一个方便的构造函数。因为微博和类名微博一样,虽然情况不同。我需要把名字改成getWeibo来解决这个问题以支持Swift。

Thanks for every one contributing to this answer. Special thanks to @Anil and @David

感谢每一个为这个答案做出贡献的人。特别感谢@Anil和@David

Jake

杰克

4 个解决方案

#1


18  

+ (Weibo*)weibo; is the class method of your class Weibo. You could access it in swift like

+(微博)微博;是您的类微博的类方法。你可以快速访问它

let w:Weibo = Weibo.weibo()

But it gives me error when i tried('weibo' is unavailable: use object construction 'Weibo()') may be because of method name and class name are same. When i change the method name error goes

但是当我尝试的时候它给了我错误(“weibo”不可用:use object construction”“weibo()”)可能是因为方法名和类名相同。当我改变方法名时,错误就会发生

let w:Weibo = Weibo.getWeibo() // works: method name changed

#2


4  

That's a class method not a property.

这是一个类方法,不是属性。

So you would access it like...

所以你可以像…

let w = Weibo.weibo()

... I think.

…我认为。

Type would be inferred but you could do it as...

类型可以推断,但你可以做…

let w:Weibo = Weibo.weibo() as Weibo

I believe, and it would still work.

我相信,它仍然会起作用。

#3


1  

I have the same problem by change code

我也有同样的问题

+(instancetype _Nonnull)manager;

you should change it like this

你应该这样改变它

@property(nonatomic, class, strong, readonly) CKKNetManager *_Nonnull manager;

then you can call in Swift like this

然后你可以像这样迅速打电话。

CKKNetManager.manager.get(kGetCompanyInfo, success: {[unowned self] (obj) in

        self.hideEmptyView()
        self.info = CKKCorpInfo.init(dictionary: obj as! [String : AnyObject])
    }) {[unowned self]  (errorString) in

        self.showEmptyView(in: self.view, with: EmptyViewType.pageLoadFailed)
    }

#4


0  

In case anyone has the same problems as I had in the past, the reason why I could not access the function from swift is because it was missing the function declaration in the .h (header) file. So it was only in the .m file and not accessible from my swift file.

如果有人有和我以前一样的问题,我不能从swift访问函数的原因是它缺少.h (header)文件中的函数声明。所以它只在。m文件中,我的swift文件无法访问。

#1


18  

+ (Weibo*)weibo; is the class method of your class Weibo. You could access it in swift like

+(微博)微博;是您的类微博的类方法。你可以快速访问它

let w:Weibo = Weibo.weibo()

But it gives me error when i tried('weibo' is unavailable: use object construction 'Weibo()') may be because of method name and class name are same. When i change the method name error goes

但是当我尝试的时候它给了我错误(“weibo”不可用:use object construction”“weibo()”)可能是因为方法名和类名相同。当我改变方法名时,错误就会发生

let w:Weibo = Weibo.getWeibo() // works: method name changed

#2


4  

That's a class method not a property.

这是一个类方法,不是属性。

So you would access it like...

所以你可以像…

let w = Weibo.weibo()

... I think.

…我认为。

Type would be inferred but you could do it as...

类型可以推断,但你可以做…

let w:Weibo = Weibo.weibo() as Weibo

I believe, and it would still work.

我相信,它仍然会起作用。

#3


1  

I have the same problem by change code

我也有同样的问题

+(instancetype _Nonnull)manager;

you should change it like this

你应该这样改变它

@property(nonatomic, class, strong, readonly) CKKNetManager *_Nonnull manager;

then you can call in Swift like this

然后你可以像这样迅速打电话。

CKKNetManager.manager.get(kGetCompanyInfo, success: {[unowned self] (obj) in

        self.hideEmptyView()
        self.info = CKKCorpInfo.init(dictionary: obj as! [String : AnyObject])
    }) {[unowned self]  (errorString) in

        self.showEmptyView(in: self.view, with: EmptyViewType.pageLoadFailed)
    }

#4


0  

In case anyone has the same problems as I had in the past, the reason why I could not access the function from swift is because it was missing the function declaration in the .h (header) file. So it was only in the .m file and not accessible from my swift file.

如果有人有和我以前一样的问题,我不能从swift访问函数的原因是它缺少.h (header)文件中的函数声明。所以它只在。m文件中,我的swift文件无法访问。