var arrayorset=["0","1","21"]
Swift is type inference and type safety but how swift know the difference between the sets and array
it will be the problem when the array is needed instead of a set because sets are unordered list it cannot retrieve me list orderly
so how to solve this problem?
... how swift know the difference between the sets and array ...
["0", "1", "21"]
is an array literal, and the type inference is documented with the ExpressibleByArrayLiteral
protocol (emphasis added):
Arrays, sets, and option sets all conform to ExpressibleByArrayLiteral
...
Because Array is the default type for an array literal, without writing any other code, you can declare an array with a particular element type by providing one or more values.
Therefore
let arrayOfStrings = ["0", "1", "21"] // [String]
declares an array of strings, and an explicit type annotation is needed to declare a set of strings:
let setOfStrings: Set = ["0", "1", "21"] // Set<String>
Note that Set
is sufficient for the type annotation here, the element type String
is inferred from the array literal.