Last active
May 28, 2016 02:57
-
-
Save stuartbreckenridge/371d1bc59a41e64f6fab8e747d9de6ad to your computer and use it in GitHub Desktop.
Configuring a Constant Using Shorthand Argument Labels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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