The Swift Programming Guide (Swift 5.1 Edition) says that subscripts can be used to access members of “a collection, list, or sequence.” Collection
and Sequence
are defined protocols in Swift and are well documented. Do lists exist in Swift as a separate entity? If so, what is the syntax for a list subscript?
Asked
Active
Viewed 694 times
-4

Mark Cowan
- 93
- 7
-
Would someone care to explain the downvotes? – Mark Cowan Sep 22 '19 at 10:26
1 Answers
1
It would be better if The Swift Programming Language (the book) didn't say “ a collection, list, or sequence”. As you point out, Swift has standard Collection
and Sequence
types. It does not have any standard type named List
.
The closest thing would be Array
:
let words: Array<String> = ["Mark", "Cowan"]
// or words: [String]
let firstWord = words[0]

rob mayoff
- 375,296
- 67
- 796
- 848
-
Thanks, @rob, but an array is both a collection and a sequence. How is a list different? Or is the passage I quoted simply misleading? – Mark Cowan Sep 22 '19 at 12:48
-
1`Collection` inherits `Sequence`, so every type that conforms to `Collection` also conforms to `Sequence`. There is no type or protocol named `List` in the standard library, so the passage is misleading. – rob mayoff Sep 22 '19 at 16:49