In Swift 1.2 there is a Set object, which you can use to create a static typed Set.
I only cannot find out how to get the object at a certain index. It has a subscript
that allows you to do the following: mySet[setIndex]
.
This retrieves the object at that setIndex
. But now I want to get an object from a certain Int
index.
var sIndex = mySet.startIndex;
var myObject = mySet[sIndex];
But how do I create a SetIndex with a certain 'index'?
Similar to
String
, you have toadvance()
from.startIndex
ADDED: As of Xcode7 beta6/Swift2.0:
A
Set
is unordered, so the object at index is an undefined behavior concept.In practice, you may build an arbitrary ordered structure out of the Set, then pick an object at index in this ordered structure.
Maybe not optimal, but easy to achieve:
Note: if you need a random element, starting with Swift 4.2 you get the convenient solution: