[Solved] What is the difference between data type String and [String]?


String refers to an individual string, whereas [String] is an array of strings. I think your error may be that you have done:

var name : String = nameList [2]

Where you should have done:

var name : String = nameList[2]

I assume that in the first instance it thinks you are setting name as ‘nameList’ because of the space, so namelist[2] should make it clear that you mean the second value in the array which is an individual String.

Edit: After testing the space should not cause a problem.
Is it really:

nameList : [String] = ["Bob", "Don", "Ron"]
var name : String = nameList [2]

As opposed to:

var nameList : [String] = ["Bob", "Don", "Ron"]
var name : String = nameList [2]
    print(name)

Please provide more code so we can see the context.

4

solved What is the difference between data type String and [String]?