我正在制作一个程序,该程序接收用户输入的信用卡号并返回信用卡类型。除了当我尝试显示类型时,我已经完成了大部分工作。我一直收到错误ViewController没有名为“from”的成员。你知道问题是什么吗?
@IBAction func CardTypePressed(sender: AnyObject) {
enum CardType: String {
case None = "None"
case Visa = "Visa"
case AmericanExpress = "American Express"
case Mastercard = "Mastercard"
case Discover = "Discover"
private func regularExpression() -> NSRegularExpression {
switch self {
case .Visa:
return NSRegularExpression(pattern: "^4[0-9]{12}(?:[0-9]{3})?$", options: nil, error: nil)!
case .AmericanExpress:
return NSRegularExpression(pattern: "^3[47][0-9]{13}$", options: nil, error: nil)!
case .Mastercard:
return NSRegularExpression(pattern: "^5[1-5][0-9]{14}$", options: nil, error: nil)!
case .Discover:
return NSRegularExpression(pattern: "^6(?:011|5[0-9]{2})[0-9]{12}$", options: nil, error: nil)!
default:
return NSRegularExpression(pattern: ".*", options: nil, error: nil)!
}
}
func isValidFor(cardNumber: String) -> Bool {
let re = self.regularExpression()
let range = NSRange(0..<cardNumber.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
let matches = re.numberOfMatchesInString(cardNumber, options: nil, range: range)
return matches > 0
}
static let allValues = [Visa, AmericanExpress, Mastercard, Discover]
}
func from(cardNumber: String) -> CardType {
for type in CardType.allValues {
if type.isValidFor(cardNumber) {
return type
}
}
return CardType.None
}
var creditCardType = self.from(creditCardTextField).rawValue
typeLabel.text = "Credit Card Type: \(creditCardType)"
}