什么是Swift相当于声明`typedef SomeClass< SomeProtocol> MyType`? [英] What's the Swift equivalent of declaring `typedef SomeClass<SomeProtocol> MyType`?

查看:262
本文介绍了什么是Swift相当于声明`typedef SomeClass< SomeProtocol> MyType`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在一个主要是Objective-C的项目中编写一些Swift代码。在我们的ObjC代码中,我们有一个标头声明 typedef GPUImageOutput< GPUImageInput> MyFilter; 。然后我们可以宣布例如@property只能是 GPUImageOutput 子类,它实现 GPUImageInput

I’m currently writing some Swift code in a project that is predominately Objective-C. In our ObjC code, we have a header that declares typedef GPUImageOutput<GPUImageInput> MyFilter;. We can then declare e.g. a @property that can only be a GPUImageOutput subclass that implements GPUImageInput.

(注意: GPUImageOutput GPUImageInput 不是我定义的;它们是 GPUImage库)

(NOTE: GPUImageOutput and GPUImageInput are not defined by me; they are part of the GPUImage library)

我们的Swift代码看起来不像认识到这一点,即使标题在我们的桥接标题中被#imported。我试图在Swift中复制声明,但这些都不是正确的语法:

typealias MyFilter = GPUImageOutput,GPUImageInput

typealias MyFilter = GPUImageOutput:GPUImageInput

Our Swift code doesn't seem to recognize this, even though the header is #imported in our Bridging Header. I’ve tried to replicate the declaration in Swift, but neither of these are proper syntax:
typealias MyFilter = GPUImageOutput, GPUImageInput
typealias MyFilter = GPUImageOutput : GPUImageInput

推荐答案

在Swift 4中你可以用新的& amp; sign(下面是一个确认为UIViewController和UITableViewDataSource的参数示例:

In Swift 4 you can achieve this with the new & sign (Below an example of a parameter confirming to UIViewController and UITableViewDataSource:

func foo(vc: UIViewController & UITableViewDataSource) {
    // access UIViewController property
    let view = vc.view
    // call UITableViewDataSource method
    let sections = vc.numberOfSectionsInTableView?(tableView)
}

这篇关于什么是Swift相当于声明`typedef SomeClass&lt; SomeProtocol&gt; MyType`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆