问题
@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?
回答1:
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.
回答2:
You can use the comma operator when you want to include multiple expressions in a location that requires a single expression.
The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.
Syntax expr1, expr2...
来源:https://stackoverflow.com/questions/30229844/comma-usage-in-swift