获取对数组的第一个元素(Swift) [英] Get first element of array of pairs (Swift)
本文介绍了获取对数组的第一个元素(Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个数组:
let arrayOfPairs = [
(8, 1),
(6, 4),
(2, 3),
]
我想要这个数组:
let arrayOfSingleValues = [
8,
6,
2,
]
如何从一对中提取一个值?
How do I pull a value out of a pair?
推荐答案
您可以使用 map 来获取元组的第一个元素,如下所示:
You can use map to get just the first element of your tuple as follow:
let arrayOfPairs = [(8, 1), (6, 4), (2, 3)]
let arrayOfSingleValues = arrayOfPairs.map { $0.0 }
print(arrayOfSingleValues) // [8, 6, 2]
这篇关于获取对数组的第一个元素(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文