我在swift-5中用它的参数Result类型写了一个类方法,现在我想在objective-c中使用这个方法,这可能吗?如果是,如何?
@objc public class DemoClass :NSObject {
@objc public func demoMethod(completion: @escaping (Result<UIImage,Error>) ->
Void) {
//some codes
}
}
一旦我将@objc添加到方法中,它就会抛出一个错误:方法不能标记为@obqc,因为参数的类型不能在Objective-C中表示
枚举结果在Objective-C中不可访问。它是@frozen
而不是@inlinable
。
而且Objective-C枚举不能是泛型的,即Result<Success, Failure> where Failure: Error
不能暴露于Objective-C。
所以,你能做的就是如下制作一个类,并根据需要做其他事情:
@objc class AResult: NSObject {
public private(set) var success: Any?
public private(set) var failure: Error?
private override init() {
super.init()
success = nil
failure = nil
}
public convenience init<Success, Failure>(_ arg1: Success, _ arg2: Failure) where Failure: Error {
self.init()
success = arg1
failure = arg2
// Do something
}
}
并声明您的函数:@objc func demoMethod(_ completion: (AResult) -> Void) {}
希望这能有所帮助。