简体   繁体   中英

App crashes when button tapped with testflight

I'm developing app with Swift 4.2 and I have problems with iOS app crash. For me everything works fine, but it doesn't work for others who installed app with testflight. Error is like this. 在此处输入图片说明

Here's a code that adds handler.

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

Here is a button click handler.

@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)
    }

}

Can anyone please help me? Thanks.

As others suggested any ! is a potential crash. Try to replace it with guard or default values. For example you can change:

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

To solution with default values:

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

Or even better guard and eg display alert if data aren't available:

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...

Please use below function and resolve an issue. I think it is possible because of the force cast string and int value.

@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)
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM