Created
October 15, 2018 03:49
-
-
Save vialyx/498c4f35fc1fd3621a3920ffa859488d to your computer and use it in GitHub Desktop.
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
extension CreditCard { | |
enum CardType { | |
case visa, unknown | |
} | |
var type: CardType { | |
guard number.count >= 1 else { return .unknown } | |
let firstNumber = number[number.startIndex..<number.index(number.startIndex, offsetBy: 1)] | |
switch firstNumber { | |
case "4": | |
return .visa | |
default: | |
return .unknown | |
} | |
} | |
} | |
let visaCard = CreditCard(number: "4111 0000 0000 0000", date: "03/05", ccv: "098") | |
print("card type: \(visaCard.type)") | |
// card type: visa |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment