类型的ViewController不符合协议
问题描述:
我试图实施该解决方案来处理与协议Protocol-Oriented Segue Identifiers in Swift多个SEGUE标识符,但是我得到这个错误:类型的ViewController不符合协议
type 'ViewController', doesn't conform to protocol '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下载项目时,它工作正常。这迫使我坚持下去。
错误:
答
该错误可能是混淆的措辞,但它的意思是,你需要确保你正在实现你的ViewController
类的方法和变量(只SegueIdentifier
枚举在这种情况下)。这样做,你应该很好去。
答
协议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.您显示的代码不包含“ViewController”。 2.错误发生在哪里? – luk2302
sory,编辑... – i6x86
@ i6x86你得到的错误意味着你还没有实现'SegueHandlerType'所需的方法和变量 – kabiroberai