Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In Swift 1.2 there is a Set object, which you can use to create a static typed Set.

I 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 myObject = mySet[sIndex];

But how do I create a SetIndex with a certain 'index'?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
825 views
Welcome To Ask or Share your Answers For Others

1 Answer

Swift 3 and newer

You can offsetBy: from .startIndex:

let mySet: Set = ["a", "b", "c", "d"]
mySet[mySet.index(mySet.startIndex, offsetBy: 2)] // -> something from the set.

Swift 2 (obsolete)

You can advancedBy() from .startIndex:

let mySet: Set = ["a", "b", "c", "d"]
mySet[mySet.startIndex.advancedBy(2)] // -> something from the set.

Swift 1.x (obsolete)

Similar to String, you have to advance() from .startIndex:

let mySet: Set = ["a", "b", "c", "d"]
mySet[advance(mySet.startIndex, 2)] // -> something from the set.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...