Skip to content

Instantly share code, notes, and snippets.

@vialyx
Created October 15, 2018 03:49
Show Gist options
  • Save vialyx/498c4f35fc1fd3621a3920ffa859488d to your computer and use it in GitHub Desktop.
Save vialyx/498c4f35fc1fd3621a3920ffa859488d to your computer and use it in GitHub Desktop.
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