简体   繁体   English

迅速调用Objective-C类方法

[英]call objective-c class method in swift

My iOS project contains both objective-c code and swift code. 我的iOS项目同时包含Objective-C代码和Swift代码。

Say, I have an class named Task , it contains a class method do : 说,我有一个名为Task的类,它包含一个类方法 do

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

The above @interface declaration is in Task.h file. 上面的@interface声明位于Task.h文件中。 Other objective-c classes are able to call the method without any problem. 其他Objective-C类也可以毫无问题地调用该方法。

My swift code want to call the doSomething method, 我的快速代码想调用doSomething方法,

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? 快速代码中调用Objective-C类方法的正确方法是什么?

(There are no problems with the generated bridging interfaces between objective-c and swift.) (objective-c和swift之间生成的桥接接口没有问题。)

I think the problem is that "do" is a reserved keyword. 我认为问题是“ do”是保留关键字。 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. 我尝试了此操作,但得到了一个略有不同的错误(关于在while循环中缺少括号),但是将方法更改为“ doSomething”可以解决此问题。

I was able to reproduce the error you saw by moving the @interface declaration to Task.m rather than Task.h. 通过将@interface声明移至Task.m而不是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. 请确认此代码的位置,因为只有在.h文件中,该代码才对其他类可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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