获取 Set<T> 中索引处的对象

Get object at index in Setlt;Tgt;(获取 SetT 中索引处的对象)
本文介绍了获取 Set<T> 中索引处的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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'?

推荐答案

Swift3及更新版本

您可以offsetBy:来自 .startIndex:

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

Swift2(过时)

你可以advancedBy() from .startIndex:

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

Swift1.x(过时)

类似于String,你必须从.startIndex advance():

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

这篇关于获取 Set<T> 中索引处的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
appearance().setBackgroundImage Not Working On Custom Class(外观().setBackoundImage在自定义类上不起作用)
Show/Hide barButtonItem(显示/隐藏barButtonItem)
Viewpager2 transistion stuck halfway when slide opened using setCurrentItem(使用setCurrentItem打开幻灯片时,ViewPager2转场中途停滞)