I have written a class method in swift-5 with it's parameter Result type, now I want to use this method in objective-c, is it possible? if yes, how?
@objc public class DemoClass :NSObject {
@objc public func demoMethod(completion: @escaping (Result<UIImage,Error>) ->
Void) {
//some codes
}
}
As soon as I add @objc to method it throws an error: Method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C
The enum Result is inaccessible in Objective-C. It is @frozen
and not @inlinable
.
And also Objective-C enums cannot be generic, ie, the Result<Success, Failure> where Failure: Error
cannot to exposed to Objective-C.
So, what can you do is make a class as follows and do other stuffs as required:
@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
}
}
And declare your function: @objc func demoMethod(_ completion: (AResult) -> Void) {}
Hope this helps.
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.