有没有办法在 XCode 中写这个而不用 2000 if else 语句?
Is there a way of writing this in XCode without 2000 if else statements?
我正在为我在 XCode 的健身房写一个签到应用程序。一年半前我问过 Android:
但是现在,我的会员已经达到 2000 人,需要添加更多。不幸的是,很明显,当我尝试添加更多行时,Xcode 崩溃了。
我的代码是这样的:
@IBAction func displayBarcode(_ sender: UIButton) {
if nameTextField.text=="100001" {
imageView.image = UIImage(named: ("All Barcodes/a0001.jpg"));
}
else if nameTextField.text=="100002" { imageView.image = UIImage(named: ("All Barcodes/a0002.jpg"));}
else if nameTextField.text=="100003" { imageView.image = UIImage(named: ("All Barcodes/a0003.jpg"));}
else if nameTextField.text=="100004" { imageView.image = UIImage(named: ("All Barcodes/a0004.jpg"));}
else if nameTextField.text=="100005" { imageView.image = UIImage(named: ("All Barcodes/a0005.jpg"));}
...一直到 2000 年。我知道,这太疯狂了,但它奏效了。有没有办法将这些组合成一个 if/else 语句到一个范围内,类似于有人给我的 Android?
的解决方案
衷心感谢!
下面是拆分字符串并在图像名称中使用其中一部分的基本解决方案:
@IBAction func displayBarcode(_ sender: UIButton) {
guard let input = nameTextField.text else {
return
}
guard input.count == 6, input.prefix(2) == "10" else {
return
}
let imageName = "All Barcodes/a\(input.suffix(4)).jpg"
if let image = UIImage(named: imageName) {
imageView.image = image
}
}
这会做一些非常的琐碎验证(例如count == 6
并检查10
的前缀),如果需要,您可以更严格它,但这适用于基本任务。
只需获取最后 4 位数字,转换为整数,检查您想要的范围是否包含您的值并检查它们是否都是数字:
let string = "100002"
let suffix = string.suffix(4)
let range = 1...2000
if let int = Int(suffix),
suffix.count == 4,
suffix.allSatisfy(("0"..."9").contains),
range ~= int {
let imageName = "All Barcodes/a\(suffix).jpg"
print(imageName)
}
这将打印
All Barcodes/a0002.jpg
我正在为我在 XCode 的健身房写一个签到应用程序。一年半前我问过 Android:
但是现在,我的会员已经达到 2000 人,需要添加更多。不幸的是,很明显,当我尝试添加更多行时,Xcode 崩溃了。
我的代码是这样的:
@IBAction func displayBarcode(_ sender: UIButton) {
if nameTextField.text=="100001" {
imageView.image = UIImage(named: ("All Barcodes/a0001.jpg"));
}
else if nameTextField.text=="100002" { imageView.image = UIImage(named: ("All Barcodes/a0002.jpg"));}
else if nameTextField.text=="100003" { imageView.image = UIImage(named: ("All Barcodes/a0003.jpg"));}
else if nameTextField.text=="100004" { imageView.image = UIImage(named: ("All Barcodes/a0004.jpg"));}
else if nameTextField.text=="100005" { imageView.image = UIImage(named: ("All Barcodes/a0005.jpg"));}
...一直到 2000 年。我知道,这太疯狂了,但它奏效了。有没有办法将这些组合成一个 if/else 语句到一个范围内,类似于有人给我的 Android?
的解决方案衷心感谢!
下面是拆分字符串并在图像名称中使用其中一部分的基本解决方案:
@IBAction func displayBarcode(_ sender: UIButton) {
guard let input = nameTextField.text else {
return
}
guard input.count == 6, input.prefix(2) == "10" else {
return
}
let imageName = "All Barcodes/a\(input.suffix(4)).jpg"
if let image = UIImage(named: imageName) {
imageView.image = image
}
}
这会做一些非常的琐碎验证(例如count == 6
并检查10
的前缀),如果需要,您可以更严格它,但这适用于基本任务。
只需获取最后 4 位数字,转换为整数,检查您想要的范围是否包含您的值并检查它们是否都是数字:
let string = "100002"
let suffix = string.suffix(4)
let range = 1...2000
if let int = Int(suffix),
suffix.count == 4,
suffix.allSatisfy(("0"..."9").contains),
range ~= int {
let imageName = "All Barcodes/a\(suffix).jpg"
print(imageName)
}
这将打印
All Barcodes/a0002.jpg