我对如何将Objective-C方法签名转换为其Swift 3.0等价物感到困惑。
以下是使用返回类型' id'的原始Objective-C方法。使用其UIViewControllerAnimatedTransitioning协议:
目标C
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
return self;
}
我希望将Objective-C版本转换为相应的Swift 3.0等价物:
Swift 3.0
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
Swift 3.0版本是否正确?
...或者我应该使用 AnyObject?返回类型:
更具体地说,应该&#39; id&#39;被翻译成协议?类型?
什么是正确的格式?
答案 0 :(得分:2)
是的,在将id<SomeProtocol>
从ObjC翻译为Swift时,协议名称就是完整类型。 (你不需要翻译任何符合协议的东西id
。协议来自ObjC,所以唯一符合它的东西就是对象 - 也就是说,你的协议隐含了AnyObject
的子类型。)
在您的情况下,optional func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
也是the official translation of that delegate method signature。
将方法声明为返回AnyObject?
会破坏委托方法签名,这将无法正常工作,因为它与正确的方法共享相同的ObjC选择器。 (并且使不正确的签名@nonobjc
保证不会被调用。)
答案 1 :(得分:0)