类型ViewController不符合协议

时间:2016-04-24 14:59:53

标签: swift

我尝试使用协议Protocol-Oriented Segue Identifiers in Swift来实现此解决方案以处理多个segue标识符,但我收到此错误:

  

键入' ViewController',不符合协议' SegueHandlerType'

以下是代码:

protocol SegueHandlerType {
    associatedtype SegueIdentifier: RawRepresentable
}
extension SegueHandlerType where Self: UIViewController, SegueIdentifier.RawValue == String {

    func performSegueWithIdentifier(segueIdentifier: SegueIdentifier,
                                    sender: AnyObject?) {

        performSegueWithIdentifier(segueIdentifier.rawValue, sender: sender)
    }

    func segueIdentifierForSegue(segue: UIStoryboardSegue) -> SegueIdentifier {

        // still have to use guard stuff here, but at least you're
        // extracting it this time
        guard let identifier = segue.identifier,
            segueIdentifier = SegueIdentifier(rawValue: identifier) else {
                fatalError("Invalid segue identifier \(segue.identifier).") }

        return segueIdentifier
    }
}

我复制/粘贴了解决方案,但结果仍然相同。最奇怪的是,当我从GitHub下载项目时,它工作正常。这让我疯狂。

错误:enter image description here

2 个答案:

答案 0 :(得分:0)

协议SegueHandlerType包含行SegueIdentifier: RawRepresentable。这意味着符合协议的类必须定义嵌套类型SegueIdentifier

本教程包含以下内容:

// the compiler will now complain if you don't have this implemented
// you need this to conform to SegueHandlerType
enum SegueIdentifier: String {
    case TheRedPillExperience
    case TheBluePillExperience
}

如果添加该代码,编译器将不再抱怨。

class ViewCtr : UIViewController, SegueHandlerType {
    enum SegueIdentifier: String {
        case YourSegueIdentifiersGoHere
    }
}

答案 1 :(得分:0)

错误可能令人困惑,但这意味着您需要确保在SegueIdentifier类中实现方法和变量(在本例中仅为ViewController枚举) 。那样做,你应该好好去。

相关问题