Tevfik Xung
Tevfik Xung

Reputation: 978

'bool' is not convertible to 'uint8'

I'm trying to build to-do app using Xcode 6 and Swift. I was able to run the app on Xcode 6 dp2 but after updating to dp7 I'm getting this error:

'bool' is not convertible to 'uint8'.

Here is the function with the error:

 override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {

        **if ((segue && segue!.identifier == "showdetails") != nil)**{
            var selectedIndexPath:NSIndexPath = self.tableView.indexPathForSelectedRow()

            var detailViewController:DetailViewController = segue!.destinationViewController as DetailViewController

            detailViewController.toDoData = toDoItems.objectAtIndex(selectedIndexPath.row) as NSDictionary

        }

    }

Upvotes: 0

Views: 1352

Answers (2)

Antonio
Antonio

Reputation: 72750

The correct way to write that is:

if (segue != nil && segue!.identifier == "showdetails") {

but an even better way is using optional binding:

if let segue = segue {
    if (segue.identifier == "showdetails") {

Note that there are other errors about incorrect usage of optionals. This is the modified code that compiles in playground:

if let segue = segue {
    if (segue.identifier == "showdetails") {
        var selectedIndexPath:NSIndexPath? = self.tableView.indexPathForSelectedRow()
        if let selectedIndexPath = selectedIndexPath {
            var detailViewController:DetailViewController = segue.destinationViewController as DetailViewController

            detailViewController.toDoData = toDoItems.objectAtIndex(selectedIndexPath.row) as NSDictionary
        }
    }
}

indexPathForSelectedRow returns an optional, so you have to account for that.

Update: as pointed out by @MartinR, segue is no longer optional, so you can solve the problem by simply updating the function signature, and its implementation should look like:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "showdetails") {
        var selectedIndexPath = self.tableView.indexPathForSelectedRow()
        if let selectedIndexPath = selectedIndexPath {
            var detailViewController = segue.destinationViewController as DetailViewController                
            detailViewController.toDoData = toDoItems.objectAtIndex(selectedIndexPath.row) as NSDictionary
        }
    }
}

Upvotes: 1

Francescu
Francescu

Reputation: 17054

First of all when I read the documentation what I read is :

func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject?)

UIKit seems to be updated in Beta7 to use less optionals. So segue may be not optional.

Then even if your function signature is good, you are comparing a Boolean to nil.

This (segue && segue!.identifier == "showdetails") is a boolean. A boolean is either true or false.

And at the very end, here is the best practice to unwrap a variable :

if let mySafeVariable = myOptionalVariable {
    if mySafeVariable.attribute == <Whatever> {

   }
}

You should. No, wait, you must read the Swift free iBooks.

Upvotes: 0

Related Questions