Search code examples
swiftswift4string-interpolation

Warnings from String interpolation


I've encountered a problem I can't solve myself. I have tried the Internet without any luck.

I'm still pretty new to Swift and coding, and right now following a guide helping me create an app.

Unfortunately, as I can understand, the app was written for Swift 3, and is giving me some issues since I'm using Swift 4.

I have to lines that gives me this warning:

String interpolation produces a debug description for an optional value; did you mean to make this explicit?

Use 'String(describing:)' to silence this warning Fix

Provide a default value to avoid this warning Fix

However, when I click one of Xcode's solutions I get another problem.

If I use the first fix, the app crashes and I get the following message:

Thread 1: Fatal error: Unexpected Segue Identifier;

If I use the second fix I have to assign a default value. And I don't know what this should be.

The whole passage of code is as follows. It's the line starting with guard let selectedMealCell and the last one after default: that is causing the issues.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    
    switch(segue.identifier ?? "") {
    case "AddItem":
        os_log("Adding a new meal.", log: OSLog.default, type: .debug)

    case "ShowDetail":
        guard let mealDetailViewController = segue.destination as? MealViewController else {
            fatalError("Unexpected destination: \(segue.destination)")
        }
        
        guard let selectedMealCell = sender as? MealTableViewCell else {
            fatalError("Unexpected sender: \(sender)")
        }
        
        guard let indexPath = tableView.indexPath(for: selectedMealCell) else {
            fatalError("The selected cell is not being displayed by the table")
        }
        
        let selectedMeal = meals[indexPath.row]
        mealDetailViewController.meal = selectedMeal
        
    default:
        fatalError("Unexpected Segue Identifier; \(segue.identifier)")
    }
}

Solution

  • So, the first suggested fix worked for you. It quieted the compile time warning, although admittedly String(describing:) is a weak solution.

    In both cases, you need to unwrap the optional value. For the first case you should use:

    guard let selectedMealCell = sender as? MealTableViewCell else {
        if let sender = sender {
            fatalError("Unexpected sender: \(sender))")
        } else {
            fatalError("sender is nil")
        }
    }
    

    and in the second case:

    fatalError("Unexpected Segue Identifier; \(segue.identifier ?? "")")
    

    Then you got a runtime error:

    "Unexpected Segue Identifier;"

    That is telling you that your switch didn't match the first 2 cases and it ran the default case. The crash is caused because your code is explicitly calling fatalError. Your segue.identifier is apparently an empty string.

    So your problem is actually in your Storyboard. You need to assign identifiers to your segues. Click on the segue arrows between your view controllers, and assign identifiers "AddItem" and "ShowDetail" to the proper segues. The segue identifier is assigned in the Attributes Inspector on the right in Xcode.