繁体   English   中英

类型 ViewController 不符合协议

[英]type ViewController doesn't conform to protocol

我正在尝试实施此解决方案,以在 Swift 中使用面向协议的 Segue Identifiers协议处理多个 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 下载项目时,它运行良好。 这让我发疯。

错误:在此处输入图片说明

协议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
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM