Comma Usage in Swift

Commas at line end in Swift

Basically it's a bug in Fix-It and the compiler's interpretation of your mistake. Your mistake is really that you've forgotten the final right parenthesis:

var firstRandomNumber = Int(arc4random_uniform(UInt32(playerArray.count)))
^

But the compiler doesn't quite grasp that, and interprets it as a missing comma:

Sample Image

These messages might be improved in a future version of Swift.

Separating multiple if conditions with commas in Swift

Actually the result is not the same. Say that you have 2 statements in an if and && between them. If in the first one you create a let using optional binding, you won't be able to see it in the second statement. Instead, using a comma, you will.

Comma example:

if let cell = tableView.cellForRow(at: IndexPath(row: n, section: 0)), cell.isSelected {
//Everything ok
}

&& Example:

if let cell = tableView.cellForRow(at: IndexPath(row: n, section: 0)) && cell.isSelected {
//ERROR: Use of unresolved identifier 'cell'
}

Hope this helps.

What does a comma mean inside of a guard clause?

, is almost the same as &&.

if 1 == 1, 2 == 2 {
print("dd")
}

if 1 == 1 && 2 == 2 {
print("dd")
}

Both of the above if statements will print dd.

, can be used wherever && can be used, like while, if and guard.

However, with if let or guard let, as the left hand side does not return a Bool, && can't be used and , must be used.

error: test.playground:4:12: error: optional type 'String?' cannot be used as a boolean; test for '!= nil' instead
if let a = a && 2 == 2 {
^
( != nil)

Swift if statement - multiple conditions separated by commas?

Yes when you write

if let a = optA, let b = optB, let c = optC {

}

Swift does execute the body of the IF only if all the optional bindings are properly completed.

More

Another feature of this technique: the assignments are done in order.

So only if a value is properly assigned to a, Swift tries to assign a value to b. And so on.

This allows you to use the previous defined variable/constant like this

if let a = optA, let b = a.optB {

}

In this case (in second assignment) we are safely using a because we know that if that code is executed, then a has been populated with a valid value.

How do I deal with commas when writing Objects to CSV in Swift?

The CSV specification suggests to wrap all fields containing special characters in double quotes.

Can if let be comma-separated in Swift 3.1?

Yes, the following code compiles with Xcode 8 and "legacy mode" (i.e.
Swift 2.3):

let a: Int? = 1
let b: Int? = 2

if let x = a, y = b { }

Starting with Swift 3 it has to be written as

if let x = a, let y = b { }

as a consequence of SE-0099 Restructuring Condition Clauses, in particular (emphasis added):

The root problem lies in the condition grammar: commas are used both to separate items within a clause (e.g. in if let x = a, y = b {) and to separate mixed kinds of clauses (e.g. if let x = a, case y? = b {). This proposal resolves this problem by retaining commas as separators between clauses (as used elsewhere in Swift) and limits clauses to single items.



Related Topics



Leave a reply



Submit