2016-04-24 128 views
-1

我试图实施该解决方案来处理与协议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下载项目时,它工作正常。这迫使我坚持下去。

错误:enter image description here

+0

1.您显示的代码不包含“ViewController”。 2.错误发生在哪里? – luk2302

+0

sory,编辑... – i6x86

+0

@ i6x86你得到的错误意味着你还没有实现'SegueHandlerType'所需的方法和变量 – kabiroberai

回答

0

该错误可能是混淆的措辞,但它的意思是,你需要确保你正在实现你的ViewController类的方法和变量(只SegueIdentifier枚举在这种情况下)。这样做,你应该很好去。

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 
    } 
} 
+0

我没有读过评论:我的错误,我感到很蠢。 – i6x86

+0

@ i6x86现在可以工作吗?如果我的答案解决了您的问题,请点击左边的复选标记 – luk2302

+0

yes接受它,但我非常抱歉,但如果您阅读了评论(在我的愚蠢问题下),您会注意到技术上@kabiroberai首先回答,所以我选择他的答案。非常感谢你! – i6x86

相关问题