相关疑难解决方法(0)

强制转换,即使协议需要给定类型

我有以下代码:

import UIKit

protocol Fooable: class where Self: UIViewController {
    func foo()
}

class SampleViewController: UIViewController, Fooable {

    func foo() {
        print("foo")
    }
}

let vc1: Fooable = SampleViewController()
let vc2: Fooable = SampleViewController()


// vc1.show(vc2, sender: nil) - error: Value of type 'Fooable' has no member 'show'

// (vc1 as! UIViewController).show(vc2, sender: nil) - error: Cannot convert value of type 'Fooable' to expected argument type 'UIViewController'

(vc1 as! UIViewController).show((vc2 as! UIViewController), sender: nil)
Run Code Online (Sandbox Code Playgroud)

注释行不编译.

为什么UIViewController即使Fooable协议需要,我也被迫将协议类型对象强制转换为符合它的类型继承 …

swift swift4

13
推荐指数
2
解决办法
657
查看次数

标签 统计

swift ×1

swift4 ×1