Search code examples
iosswiftnsfetchedresultscontroller

Unsure of use of ?? in Swift


I'm using this in my UITableViewController datasource to return the number of sections, as defined by my NSFetchedResultsController.

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return self.fetchedResultsController.sections?.count ?? 0
}

I've stumbled across this from samples on Swift and it works, but am unsure what the double ? means/does.

Help appreciated.


Solution

  • It is called the nil coalescing operator, and it is shorthand for the following:

    return self.fetchedResultsController.sections != nil ? self.fetchedResultsController.sections! : 0