i6x86 i6x86 - 7 months ago 15
Swift Question

type ViewController doesn't conform to protocol

i'm trying to implement this solution to handle multiple segue identifiers with protocol Protocol-Oriented Segue Identifiers in Swift , but i got this error:


type 'ViewController', doesn't conform to protocol 'SegueHandlerType'


Here is the code:

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


i copy/pasted the solution but still the same result. and the strangest thing is that when i downloaded the project from the GitHub, it works fine. this drives me nuts.

the error: enter image description here

Answer

The protocol SegueHandlerType contains the line SegueIdentifier: RawRepresentable. That means that the class conforming to the protocol has to define a nested type SegueIdentifier.

The tutorial includes the following for that matter:

// 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
}

If you add that code the compiler will not longer complain.

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