Skip to content

Instantly share code, notes, and snippets.

@juliengdt
Created January 16, 2018 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliengdt/ea16dca7f17a03e26b395adc982fdd3b to your computer and use it in GitHub Desktop.
Save juliengdt/ea16dca7f17a03e26b395adc982fdd3b to your computer and use it in GitHub Desktop.
Protocol For Enumeration - Swift 4
//: Playground - noun: a place where people can play
import UIKit
import Foundation
var str = "Hello, playground"
protocol JSONRepresentable {
var JSONRepresentation: AnyObject{ get }
}
protocol ParkingProviderProtocol: JSONRepresentable {
associatedtype TypeSelf
associatedtype RawType
var value: RawType { get }
var stopToPlaceCar: Bool { get }
var description: String { get }
static func with(intValue: RawType?) -> TypeSelf
static var preferedOption: TypeSelf { get }
static var all: [TypeSelf] { get }
}
enum ParkingProvider: ParkingProviderProtocol {
typealias TypeSelf = ParkingProvider
typealias RawType = Int
case justDropOff
case greenP
case parkAndRide
var value: RawType {
switch self {
case .justDropOff:
return -1
case .greenP:
return 217
case .parkAndRide:
return 6
}
}
var stopToPlaceCar: Bool {
switch self {
case .justDropOff: return false
case .parkAndRide: return true
case .greenP: return true
}
}
public var description: String {
switch self {
case .justDropOff: return "Get dropped off at stop"
case .parkAndRide: return "Park at park and rides only_car"
case .greenP: return "Park at Green P only"
}
}
static func with(intValue: Int?) -> TypeSelf {
guard let _rawValue = intValue else { return .justDropOff}
switch _rawValue {
case TypeSelf.justDropOff.value:
return .justDropOff
case TypeSelf.greenP.value:
return .greenP
case TypeSelf.parkAndRide.value:
return .parkAndRide
default:
return .justDropOff
}
}
static var preferedOption: TypeSelf {
return .justDropOff
}
static var all: [TypeSelf] = [.justDropOff , .greenP, .parkAndRide]
/* JSONrepresentable */
var JSONRepresentation: AnyObject { return self.value as AnyObject }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment