What's the difference between [String] and [(String)]?

What's the difference between [String] and [(String)] in Swift? I get them with let arr1 : [String]

and let arr2 = [String]()

.

+3


source to share


2 answers


[String] is an array of strings and [(String)] is an array of tuples containing 1 string.

let arr1 = [("test")]
arr1[0].0 // contains "test"

let arr2 = ["test"]
arr2[0] // contains "test"

      



A more useful array of tuples might be something like this:

let arr1: [(id: Int, name: String)] = [(12, "Fred"), (43, "Bob")]
arr1[0].name // contains "Fred"

      

+7


source


There shouldn't be a difference - it's a glitch somewhere in Xcode or Swift if you see [(String)]

. In theory, all variables are singleton tuples - so every variable has a property .0

:

let s = "hello"
print(s.0)

      



but in practice this should be normalized so that you never see (OneThing)

, only ever OneThing

.

+9


source







All Articles