Skip to content

Instantly share code, notes, and snippets.

@stuartbreckenridge
Last active May 28, 2016 02:57
Show Gist options
  • Save stuartbreckenridge/371d1bc59a41e64f6fab8e747d9de6ad to your computer and use it in GitHub Desktop.
Save stuartbreckenridge/371d1bc59a41e64f6fab8e747d9de6ad to your computer and use it in GitHub Desktop.
Configuring a Constant Using Shorthand Argument Labels
// See https://www.natashatherobot.com/swift-configuring-a-constant-using-shorthand-argument-names/ for more info.
public enum TaxiServiceType:CustomStringConvertible
{
case Uber
case BlackCab
public var description: String {
switch self {
case .BlackCab:
return "Black Cab"
case .Uber:
return "Uber"
}
}
}
public class TaxiService:CustomStringConvertible
{
private var serviceTypeInternal: TaxiServiceType
public var serviceType: TaxiServiceType {
get {
return self.serviceTypeInternal
}
}
private var maximumFare:Float
public init!(forServiceType type:TaxiServiceType)
{
self.serviceTypeInternal = type
self.maximumFare = 0.0
}
public func setPreferredMaxFare(fare:Float)
{
maximumFare = fare
}
public var description: String {
let formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.currencySymbol = "$"
let fare = formatter.stringFromNumber(NSNumber(float: maximumFare))!
return "You've selected a \(serviceType) service with a maximum fare of \(fare)"
}
}
let tService:TaxiService = {
$0.setPreferredMaxFare(23.00)
return $0
}(TaxiService(forServiceType: .BlackCab))
print(tService.description) // You've selected a Black Cab service with a maximum fare of $23.00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment