Ioan Moldovan
Ioan Moldovan

Reputation: 2422

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. enter image description here

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.

Upvotes: 0

Views: 215

Answers (2)

Martin Pilch
Martin Pilch

Reputation: 3295

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

Upvotes: 1

Mitul.Patel
Mitul.Patel

Reputation: 252

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

}

Upvotes: 1

Related Questions