简体   繁体   中英

call objective-c class method in swift

My iOS project contains both objective-c code and swift code.

Say, I have an class named Task , it contains a class method do :

@interface Task: NSObject
+ (void) doSomething:(NSString *) value;
@end

The above @interface declaration is in Task.h file. Other objective-c classes are able to call the method without any problem.

My swift code want to call the doSomething method,

Task.doSomething(“do task now!”)

But the above line of code get the following compiler error:

'Task.Type' does not have a member named 'doSomething'.

Why? What is the correct way to call objective-c class method in swift code?

(There are no problems with the generated bridging interfaces between objective-c and swift.)

I think the problem is that "do" is a reserved keyword. I tried this and got a slightly different error (about missing a bracket for a while loop), but changing the method to "doSomething" fixed the problem.

I was able to reproduce the error you saw by moving the @interface declaration to Task.m rather than Task.h. Please confirm where you have this code, as it will only be visible to other classes if it is in the .h file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM