Jake Lin Jake Lin - 1 month ago 8
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

weibo()
as below from Objective-C

@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.

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.

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

Jake

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