gpt4 book ai didi

ios - Swift 中的选择器 View

转载 作者:行者123 更新时间:2023-11-30 12:58:41 25 4
gpt4 key购买 nike

我在 didSelectRow 函数的选取器 View 中遇到应用程序问题

在某些情况下,选择器 View 可能为零,因此 didSelectRow 内的所有指令都将读取无值,并且应用程序崩溃。

我多次尝试使用不同的解决方案来防止此崩溃,但它不起作用:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)  {
if (self.MEDARRAY[row].value(forKey: "medNumDay") as? NSInteger) != nil {

let NumDay = self.MEDARRAY[row].value(forKey: "medNumDay") as? NSInteger
let NumMed = self.MEDARRAY[row].value(forKey: "medNum") as? NSInteger
Counter = (self.MEDARRAY[row].value(forKey: "takenCounter") as? NSInteger)!
theTimeSet = (self.MEDARRAY[row].value(forKey: "nextTime")as? Date)!
numberforday = (24 / NumDay!)
thegetting.timeStyle = DateFormatter.Style.short
TotalDose.text = String(NumDay! * NumMed!)
RemainDose.text = String(Int(TotalDose.text!)! - Counter!)
NextTimes.text = thegetting.string(from: theTimeSet!)
}
else{
TotalDose.text = "No"
RemainDose.text = "No"
NextTimes.text = "No"
}
}

再次尝试

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)  {
if (MEDARRAY.count < 1) {
let NumDay = self.MEDARRAY[row].value(forKey: "medNumDay") as? NSInteger
let NumMed = self.MEDARRAY[row].value(forKey: "medNum") as? NSInteger
Counter = (self.MEDARRAY[row].value(forKey: "takenCounter") as? NSInteger)!
theTimeSet = (self.MEDARRAY[row].value(forKey: "nextTime")as? Date)!
numberforday = (24 / NumDay!)
thegetting.timeStyle = DateFormatter.Style.short
TotalDose.text = String(NumDay! * NumMed!)
RemainDose.text = String(Int(TotalDose.text!)! - Counter!)
NextTimes.text = thegetting.string(from: theTimeSet!)
}
else{
TotalDose.text = "No"
RemainDose.text = "No"
NextTimes.text = "No"
}
}

问题是它仍然试图通过此错误:索引超出范围来获取以下代码的值

let NumDay = self.MEDARRAY[row].value(forKey: "medNumDay") as? NSInteger
let NumMed = self.MEDARRAY[row].value(forKey: "medNum") as? NSInteger
Counter = (self.MEDARRAY[row].value(forKey: "takenCounter") as? NSInteger)!
theTimeSet = (self.MEDARRAY[row].value(forKey: "nextTime")as? Date)!

最佳答案

如果您要识别的项目是可选的,但您需要它们具有非零值来执行函数,请在函数的头部放置一个保护语句来测试每个值是否可用:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
guard row < MEDARRAY.count,
MEDARRAY.count < 1,
let NumDay : NSInteger = self.MEDARRAY[row].value(forKey: "medNumDay") as NSInteger,
let NumMed = self.MEDARRAY[row].value(forKey: "medNum") as NSInteger,
let Counter = (self.MEDARRAY[row].value(forKey: "takenCounter") as NSInteger
let theTimeSet = (self.MEDARRAY[row].value(forKey: "nextTime") as Date else { print("Could not obtain all required values") ; return }

// ... function continues here...

请注意,您可以用逗号链接保护语句,并且这些常量不是可选的:如果非零值可用,它们将被初始化为非可选,否则将触发保护异常。 (另请注意对数组测试和行输入的更正。)

这样,如果您无法将这些常量初始化为非零值,该函数将直接退出,这应该可以防止应用程序崩溃。当然,这并不能解决为什么值为零的问题,但至少可以让执行继续进行。

希望有帮助。

关于ios - Swift 中的选择器 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40108851/

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