Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comma usage in swift

Tags:

swift

@IBAction func selectedGame(segue:UIStoryboardSegue) {
  if let gamePickerViewController = segue.sourceViewController as? GamePickerViewController,
    selectedGame = gamePickerViewController.selectedGame {
    detailLabel.text = selectedGame
    game = selectedGame
  }
}

Hi all, i'm following a tutorial to learn something about swift. Yesterday I found this part of code but I can not find a way to understand what it means thatcomma means. Can u pls explain me?

like image 513
Marco Rossini Avatar asked May 14 '15 05:05

Marco Rossini


1 Answers

The comma is used to combine multiple optional bindings into one statement to avoid unnecessary nesting.

From Swift 1.2,The if let construct can now unwrap multiple optionals at once, as well as include intervening boolean conditions. This lets you express conditional control flow without unnecessary nesting.More details

For example:

var foo: Int!
var bar: String!

// Swift 1.2

if let foo = foo,bar = bar {
    // foo & bar have values.
} else {

}

// before Swift 1.2

if let foo = foo {
    // nesting
    if let bar = bar {
        // foo & bar have value.
    }
}

Xcode6.3 and above support Swift1.2.

like image 65
tounaobun Avatar answered Oct 10 '22 13:10

tounaobun