用testflight轻按按钮时,应用崩溃

时间:2019-01-03 11:35:31

标签: ios iphone swift

我正在使用Swift 4.2开发应用,但是iOS应用崩溃时遇到了问题。 对我来说,一切正常,但是对于其他使用testflight安装应用程序的人而言,则无法正常工作。 错误是这样的。 enter image description here

这是添加处理程序的代码。

cell.btnBooknow.addTarget(self, action: #selector(pressBookNowButton(button:)), for: .touchUpInside)

这是一个按钮单击处理程序。

@objc func pressBookNowButton(button: UIButton) {
    guard let tagValue = self.arrListingBagHandler.object(at: button.tag) else {
        return
    }
    print(self.artworks)

    print(self.onlineStatus)
    print(self.arrSearchAddressResult)


    print(tagValue)
    let valueStatus = tagValue["IsOnline"] as! String
    let availability = tagValue["AvailabilityDays"] as! String
    let arr = Array(availableDays)
    print(valueStatus)
    print(arr)
    let arrAvailability = Array(availability)
    print(arrAvailability)
    let arr1 = availability.components(separatedBy: ",")
    print(arr1)

    let day = (presentDay!-1)
    let today = "\(day)"
    print(today)
    if arr1.contains(today)
    {
        availableToday = true
    }
    else
    {
        availableToday = false
    }

    if valueStatus == "1" && availableToday == true
    {
        let obj = self.storyboard?.instantiateViewController(withIdentifier: "bookingId") as! BookingViewController
        obj.dicBagHanlderDetail = tagValue
        self.navigationController?.pushViewController(obj, animated: true)
    }
    else 
    {
        let alert = UIAlertController(title: "Alert!", message: "You can't make booking because store is currently closed.", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .cancel) { (actoin) in
        }
        alert.addAction(okAction)
        self.present(alert, animated: true, completion: nil)
    }

}

有人可以帮助我吗? 谢谢。

2 个答案:

答案 0 :(得分:1)

正如其他人建议的那样,任何!都可能导致崩溃。尝试将其替换为保护值或默认值。例如,您可以更改:

let valueStatus = tagValue["IsOnline"] as! String
let availability = tagValue["AvailabilityDays"] as! String

要使用默认值的解决方案:

let valueStatus = tagValue["IsOnline"] as? String ?? "1"
let availability = tagValue["AvailabilityDays"] as? String ?? "5"

甚至更好的后卫,例如如果没有可用数据,则显示警报:

guard let valueStatus = tagValue["IsOnline"] as? String,
    let availability = tagValue["AvailabilityDays"] as? String else {
    // Display alert in case of unavailable data (or simply return)
    let alert = UIAlertController(title: "Alert!", message: "You can't make booking because store is currently closed.", preferredStyle: .alert)
    let okAction = UIAlertAction(title: "OK", style: .cancel) { _ in }
    alert.addAction(okAction)
    self.present(alert, animated: true, completion: nil)
    return
}
// Continue as expected here...

答案 1 :(得分:1)

请使用以下功能并解决问题。我认为这是有可能的,因为强制转换了字符串和int值。

@objc func pressBookNowButton(button: UIButton) {
guard let tagValue = self.arrListingBagHandler.object(at: button.tag) else {
    return
}




let valueStatus = tagValue["IsOnline"] as? String ?? ""
let availability = tagValue["AvailabilityDays"] as? String ?? ""
let arr = Array(availableDays)

let arrAvailability = Array(availability)

let arr1 = availability.components(separatedBy: ",")

let value : Int = Int(presentDay) ?? 0
let day = (value-1)
let today = "\(day)"

if arr1.contains(today)
{
    availableToday = true
}
else
{
    availableToday = false
}

if valueStatus == "1" && availableToday == true
{
    let obj = self.storyboard?.instantiateViewController(withIdentifier: "bookingId") as! BookingViewController
    obj.dicBagHanlderDetail = tagValue
    self.navigationController?.pushViewController(obj, animated: true)
}
else
{
    let alert = UIAlertController(title: "Alert!", message: "You can't make booking because store is currently closed.", preferredStyle: .alert)
    let okAction = UIAlertAction(title: "OK", style: .cancel) { (actoin) in
    }
    alert.addAction(okAction)
    self.present(alert, animated: true, completion: nil)
}

}