Get object at index in Setlt;Tgt;(获取 SetT 中索引处的对象)
问题描述
在 Swift 1.2 中有一个 Set 对象,您可以使用它来创建静态类型的 Set.
In Swift 1.2 there is a Set object, which you can use to create a static typed Set.
我不知道如何在某个索引处获取对象.它有一个 subscript
允许您执行以下操作:mySet[setIndex]
.
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]
.
这将检索该 setIndex
处的对象.但是现在我想从某个 Int
索引中获取一个对象.
This retrieves the object at that setIndex
. But now I want to get an object from a certain Int
index.
var myObject = mySet[sIndex];
但是如何创建具有特定索引"的 SetIndex?
But how do I create a SetIndex with a certain 'index'?
推荐答案
Swift 3 及更新版本
您可以offsetBy:
来自 .startIndex
:
let mySet: Set = ["a", "b", "c", "d"]
mySet[mySet.index(mySet.startIndex, offsetBy: 2)] // -> something from the set.
Swift 2(过时)
你可以advancedBy()
from .startIndex
:
let mySet: Set = ["a", "b", "c", "d"]
mySet[mySet.startIndex.advancedBy(2)] // -> something from the set.
Swift 1.x(过时)
类似于String
,你必须从.startIndex
advance()
:
let mySet: Set = ["a", "b", "c", "d"]
mySet[advance(mySet.startIndex, 2)] // -> something from the set.
这篇关于获取 Set<T> 中索引处的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取 Set<T> 中索引处的对象


基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01