我使用https://stackoverflow.com/a/28964059/6481734处的答案将NSIndexSet转换为[Int]数组我需要完全相反,将相同类型的数组转换回NSIndexSet。
答案 0 :(得分:94)
IndexSet
直接从数组文字创建 init(arrayLiteral:)
,如下所示:
<li class="dropdown" id="dropdown1"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown<b class="caret"></b></a>
<ul class="dropdown-menu">
<li id="li-1"><a href="#" onclick="Alert("li-1")">List1</a></li>
<li id="li-2"><a href="#" onclick="Alert("li-2")">List</a></li>
</ul>
</li>
与pbasdf's answer类似,但使用forEach(_:)
<!DOCTYPE html>
<html>
<head>
<script src="../d3/d3.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<style>
.graph_options {
background-color:rgb(50,150,60);
color:rgb(255,255,255);
text-align:center;
float:left;
width:20%;
height:50vh;
}
</style>
<body>
<div class="graph_options row-fluid">
<div class="col-sm-3">
<script type="text/javascript" src="../javascript/collision.js" ></script>
</div>
</div>
</body>
</html>
答案 1 :(得分:69)
在Swift 3中这将更容易:
let array = [1,2,3,4,5,7,8,10]
let indexSet = IndexSet(array)
哇!
答案 2 :(得分:24)
Swift 3 +
let fromRange = IndexSet(0...10)
let fromArray = IndexSet([1, 2, 3, 5, 8])
添加了此答案,因为尚未提及fromRange
选项。
答案 3 :(得分:3)
您可以使用NSMutableIndexSet
及其addIndex
方法:
let array : [Int] = [1,2,3,4,5,7,8,10]
print(array)
let indexSet = NSMutableIndexSet()
for index in array {
indexSet.addIndex(index)
}
print(indexSet)
答案 4 :(得分:1)
Swift 4.2
从现有数组中
let arr = [1, 3, 8]
let indexSet = IndexSet(arr)
来自数组文字:
let indexSet: IndexSet = [1, 3, 8]