Jake Lin Jake Lin - 7 months ago 31
iOS Question

How to access an Objective-C class method from Swift language

In my Swift app, I need to access a class method called

as below from Objective-C

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

I have configured the bridging header and tried the following statement

let w = Weibo.weibo() as Weibo

It doesn't work.

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

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
is same as the Class name
although the case is different. I need to change the name to
to fix this issue to support Swift.

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


Answer Source

+ (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

let w:Weibo = Weibo.getWeibo() // works: method name changed
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download