0

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

4
  • This may just be a type in the code posted, but the method declaration needs a semicolon in the obj-c interface. Commented Aug 18, 2015 at 14:35
  • I just forget to add the semicolon, I have semicolon in my objective-c code. Commented Aug 18, 2015 at 14:37
  • Can you clarify which file the @interface declaration you quoted is in? It will need to be in the .h file to be visible. Are other objective-c classes able to call the method? Commented Aug 18, 2015 at 14:46
  • I updated my question, the @interface declaration is in Task.h , other objective-c classes are able to call the method without any problem Commented Aug 19, 2015 at 7:03

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

3 Comments

If that were the issue, it can be solved by quoting it with backpacks: Task.`do`("do task now!")
Thanks, but do is just my example method in my question, in my real code, the name is definitely not a reserved keyword. I updated my question with doSomething to avoid this kind of misleading. Besides, my interface declaration is in its Task.h .
Can you add a copy of your bridging header to your question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.