Reputation: 5019
I have a method which accepts a block, the method signatures in objective C are
- (void) startWithFinishingBlock:(FinishingBlock _Nonnull)finishingBlock;
I have typedef FinishingBlock as below
typedef void (^FinishingBlock)(id<Operation> _Nonnull operation);
Now i am trying to call this methods from swift and having some trouble in writing it in swift. I am ending up with something like this.
operation?.start(finishing: ({(operation : AnyObject) in
} as? FinishingBlock)!)
But i know that it should not be AnyObject
, i want here some object which conforms to <Operation>
protocol. How i can define this in swift.
One additional thing that i have one Operation class and one <Operation>
protocol in my code. So basically Operation
class is conforming <Operation>
protocol
Upvotes: 1
Views: 253
Reputation: 437552
Whenever you're unsure, you can just infer the correct types, e.g.:
operation?.start { operation in
// ...
}
Just for the sake of completeness, the syntax (neither employing the above trailing closure syntax nor inferring the type) would be:
operation?.start(finishing: { (operation: MyApp.Operation) -> Void in
// ...
})
Because your Operation
protocol clashes with the Foundation Operation
type (i.e. the new name for NSOperation
in Swift 3), you have to qualify your protocol name with your target name (MyApp
in my example above). This is the advantage of inferring the type of the parameter, as you don't have to deal with this.
Regardless, if you have the opportunity, I might recommend renaming your Operation
protocol to avoid any confusion in the future.
Upvotes: 1