gpt4 book ai didi

ios - ViewController 没有名为 "allValues"的成员

转载 作者:行者123 更新时间:2023-11-29 01:55:47 25 4
gpt4 key购买 nike

这是一个常见问题,但我找不到解决我的具体问题的答案。我正在创建一个接受用户输入的信用卡并返回信用卡类型的程序。除了我不断收到错误消息“ViewController 没有成员名称‘allValues’之外,一切似乎都已检查完毕。有人知道如何解决这个问题吗?

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var typeLabel: UILabel!
@IBOutlet weak var creditCardTextField: UITextField!

@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 = CardType.from(creditCardTextField).toRaw()
typeLabel.text = "Credit Card Type: \(creditCardType)"

}
}

最佳答案

self 中没有属性allValues

您的意思可能是 CardType.allValues。像这样:

func from(cardNumber: String) -> CardType {
for type in CardType.allValues {
if type.isValidFor(cardNumber) {
return type
}
}
return CardType.None
}

关于ios - ViewController 没有名为 "allValues"的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30875972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com