[英]Can't call a method from a swift class in Objective C
我正在使用用Objective C编写的iOS应用程序,该应用程序具有用Swift编写的附加类。 当我尝试在我的Obj C AppDelegate.m中调用a时,我没有收到任何错误,但从未触发回复回调。
我的ObjC具有以下方法:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
NSString *success =[ApiController handleCall];
//NSString *success =[self hello]; // This works if you swap it with the above method
//NSString *success = @"hello"; // when uncommented this also works.
NSDictionary *dict = @{@"status" : success};
reply(dict);
}
-(NSString *)hello{
return @"hello";
}
NSString *success = [SwiftClass swiftMethod];
我的快速班级看起来像这样:
@objc class SwiftClass {
@objc func swiftMethod()->NSString{
return "it works!"
}
}
除了上述代码外,我还确保包括#import "ProjectName-Swift.h"
,并为swift代码创建桥接头。
我有什么想念的吗?
swiftMethod
是实例方法,而不是类方法。 您可以这样称呼它:
NSString *success = [[SwiftClass new] swiftMethod];
如果要将其作为类方法调用,则需要使用class
进行声明:
@objc class SwiftClass {
@objc class func swiftMethod()->NSString{
return "it works!"
}
}
另外,您必须在类和方法之前添加@objc
@objc class MyClass: NSObjec {
@objc func methodName() {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.