Swift:从元组中获取元素

时间:2021-10-02 00:34:17

If I have a tuple like this

如果我有一个这样的元组

var answer: (number: Int, good: Bool)

How do I get one of the elements?

如何得到其中一个元素?

This doesn't work:

这并不工作:

answer["number"]

I am modeling this question after Swift: Get an array of element from an array of tuples, but my question was a little more basic. I did find the answer buried in the documentation so I am adding my answer below in Q&A format for faster searching in the future.

我正在用Swift代码建模这个问题:从一个元组数组中获取一个元素数组,但我的问题更基本一些。我确实在文档中找到了答案,所以我在Q&A格式中添加了我的答案,以便在将来进行更快的搜索。

2 个解决方案

#1


42  

According to the documentation (scroll down to Tuples), there are three ways to do it.

根据文档(向下滚动到元组),有三种方法可以做到这一点。

Given

鉴于

var answer: (number: Int, good: Bool) = (100, true)

Method 1

方法1

Put the element variable name within a tuple.

将元素变量名放在一个元组中。

let (firstElement, _) = answer
let (_, secondElement) = answer

or

let (firstElement, secondElement) = answer

Method 2

方法2

Use the index.

使用索引。

let firstElement = answer.0
let secondElement = answer.1

Method 3

方法3

Use the names. This only works, of course, if the elements were named in the Tuple declaration.

使用的名字。当然,如果元素在Tuple声明中被命名,这只会起作用。

let firstElement = answer.number
let secondElement = answer.good

#2


1  

I tried this. It's not so good but works...

我试着这一点。这不是很好,但很有效……

protocol SubscriptTuple {
    associatedtype Tuple
    associatedtype Return
    var value: Tuple { get set }
    subscript(sub: String) -> Return? { get }
}

struct TupleContainer: SubscriptTuple {
    typealias Tuple = (number: Int, good: Bool)
    typealias Return = Any
    var value: Tuple
    subscript(sub: String) -> Return? {
        switch sub {
        case "number":
            return value.number
        case "good":
            return value.good
        default:
            return nil
        }
    }
}

And this is how to use.

这就是如何使用。

let answer = Answer(value: (120, false))
answer["number"]

#1


42  

According to the documentation (scroll down to Tuples), there are three ways to do it.

根据文档(向下滚动到元组),有三种方法可以做到这一点。

Given

鉴于

var answer: (number: Int, good: Bool) = (100, true)

Method 1

方法1

Put the element variable name within a tuple.

将元素变量名放在一个元组中。

let (firstElement, _) = answer
let (_, secondElement) = answer

or

let (firstElement, secondElement) = answer

Method 2

方法2

Use the index.

使用索引。

let firstElement = answer.0
let secondElement = answer.1

Method 3

方法3

Use the names. This only works, of course, if the elements were named in the Tuple declaration.

使用的名字。当然,如果元素在Tuple声明中被命名,这只会起作用。

let firstElement = answer.number
let secondElement = answer.good

#2


1  

I tried this. It's not so good but works...

我试着这一点。这不是很好,但很有效……

protocol SubscriptTuple {
    associatedtype Tuple
    associatedtype Return
    var value: Tuple { get set }
    subscript(sub: String) -> Return? { get }
}

struct TupleContainer: SubscriptTuple {
    typealias Tuple = (number: Int, good: Bool)
    typealias Return = Any
    var value: Tuple
    subscript(sub: String) -> Return? {
        switch sub {
        case "number":
            return value.number
        case "good":
            return value.good
        default:
            return nil
        }
    }
}

And this is how to use.

这就是如何使用。

let answer = Answer(value: (120, false))
answer["number"]