Understanding the syntax for an array of tuples in Swift

I am trying to understand the syntax for tuple arrays in Swift:

If I create a tuple:

var gameScore: (points: Int, player: String)

I can assign these values:

gameScore = (1700, "Lisa")

And create an array of this tuple:

var gameScores = [gameScore]

I can add to the array as follows:

gameScores.append((1650, "Bart"))

And thus:

gameScore = (1600, "Maggie")
gameScores += [gameScore]

But not like that:

gameScores += [(1600, "Maggie")]

Playground error:

Playground execution failed: error: Tuples Playground.playground: 38: 1: error: cannot convert value of type '[(points: Int, player: String)]' to the expected argument type 'inout _' gameScores + = [(1600 , "Maggie")]

However, this method works:

gameScores += [(points: 1600, player: "Maggie")]

Yes. I have the code above that will work, but I'm trying to figure out what I don't understand in the failure syntax. Elements should not be named for the method .append(), but they must be named for += [()].

+4
1

Swift . Swift [(1600, "Maggie")] . , :

gameScores += [(1600, "Maggie") as (points: Int, player: String)]

gameScores += [(1600, "Maggie")] as [(points: Int, player: String)]

gameScores = gameScores + [(1600, "Maggie")]

.

, Swift , +=.

+=:

func +=<C : Collection>(lhs: inout Array<C.Iterator.Element>, rhs: C)

, lhs rhs . Swift lhs rhs . , rhs, , lefthand inout _, gameScores, [(points: Int, player: String)]. ? , , , , :

gameScores += [(points: 1600, player: "Maggie")]
+2

Source: https://habr.com/ru/post/1657205/


All Articles