19

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

1
  • 3
    That's a class method not a property. i.e. Weibo.weibo() Commented Jun 23, 2014 at 13:38

5 Answers 5

20

+ (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 
Sign up to request clarification or add additional context in comments.

8 Comments

It depends on what the method does. The swift compiler is treating it as a convenience constructor, which is why you get the error message. If that is the case, you should just use let w = Weibo(). If, as I suspect, it's a singleton method, you'll need to change the name of the method to someing that doesn't look like a convenience constructor.
Thanks for your help, I have forked the repo and fixed it.
@David, thanks for pointing out convenience constructor. I saw it in the WWDC video before. Thanks again.
I've a class with name "MMSDK" with a class method "+ (void)initialize;", I tried to call MMSDK.initialize() but its not working. Am I missing anything. The error that I'm getting is "Expected deceleration". What's the problem?
@Satyam Have you imported the class via `bridging header?
|
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.

Comments

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) } 

1 Comment

or rename like this code + (instancetype _Nonnull)sharedManager; then call code CKKNetManager.shared().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) }
1

In my situation, my Obj-c class method returned a pointer to a new class not added to my bridging header so the compiler did not make that class method accessible.

You need both the class with the class method you want to call and any class in the class method's description to be included in your bridging header.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.