Swift中的多个通用参数

时间:2016-09-22 09:24:35

标签: ios swift generics

我希望在swift中有一个函数,它需要两个必须有一些基类的参数,每个参数都面对不同的协议。

n'

但每当我尝试使用这样的方法时:

if branch in ['develop']:
    DEBUG = True
    RAVEN_CONFIG = {
    'dsn': 'your_link_to_raven',
}
else:
    #some other settings

Xcode给我一个错误

  

无法推断通用参数“T”

中间和左侧的变量都是继承自UIViewController的类的实例,并且面对T或U所需的协议。

createContainerViewController函数中的用法:

mappings: {
    "comment": {
        "properties": {
            "content": { "type": "string" },
            "replies": { "type": "comment" }
        }
    },
    "post": {
        "properties": {
            "comments": {
                  "type": "comment"
             }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这应该有用。

protocol CenterViewController {}
protocol SidePanelViewController {}

class Center: UIViewController, CenterViewController {}
class Left: UIViewController, SidePanelViewController {}

class ContainerViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let center = Center()
        let left = Left()
        let _ = ContainerViewController.createContainerViewController(withCenterViewController: center, andLeftViewController: left)
    }

    class func createContainerViewController<T:UIViewController, U:UIViewController>(withCenterViewController centerViewController: T, andLeftViewController leftViewController: U) -> ContainerViewController where T: CenterViewController, U: SidePanelViewController {
        return ContainerViewController()
    }
}