Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c Swift:实现协议的类的类型_Objective C_Swift - Fatal编程技术网

Objective c Swift:实现协议的类的类型

Objective c Swift:实现协议的类的类型,objective-c,swift,Objective C,Swift,在Objective-C中,可以在返回值中指定类名和一组协议。例如,作为UIScrollViewDelegate的UIViewController: - (UIViewController<UIScrollViewDelegate> *) viewDelegate; -(UIViewController*)viewDelegate; 我找不到用Swift来表达这一点的方法。类似的操作失败,因为UIViewController不是协议: func viewDelegate() -&

在Objective-C中,可以在返回值中指定类名和一组协议。例如,作为UIScrollViewDelegate的UIViewController:

- (UIViewController<UIScrollViewDelegate> *) viewDelegate;
-(UIViewController*)viewDelegate;
我找不到用Swift来表达这一点的方法。类似的操作失败,因为UIViewController不是协议:

func viewDelegate() -> protocol<UIViewController, UIScrollViewDelegate>;
func viewDelegate()->协议;

您可以创建带有约束的通用函数。这是否满足您的用例

  func viewDelegate<T: UIViewController, UIScrollLViewDelegate>() -> T {

    var t = T()

    return t;

  }
func viewDelegate()->T{
var t=t()
返回t;
}

只是想知道为什么需要对返回类型进行协议声明?@Woodstock返回值需要实现来自类和协议的方法。如果类型检查器能够强制执行,而不是在运行时进行检查,那就太好了!但是我应该提到的是,这个项目仍然主要是Objective-C,它看起来像是泛型函数不向后兼容。我得到编译器错误
方法不能标记为@objc,因为它的结果类型不能在Objective-C中表示,因为它是
@objc
。标记为解决方案,因为我不认为有一种方法可以做到这一点,即ObjC兼容。