Swift: Get an element from a tuple

Suragch :

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.

Suragch :

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

Put the element variable name within a tuple.

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

or

let (firstElement, secondElement) = answer

Method 2

Use the index.

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

Method 3

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

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

transform each element of tuple; get that tuple

分類Dev

How we can get the Element from Set in Swift 2.2

分類Dev

Get element.type from generic object in swift

分類Dev

Sum the components of a tuple up by using std::get, std::tuple_size, std::tuple_element

分類Dev

How to get something specific from a tuple

分類Dev

How to return which element from the tuple matched in the endswith function

分類Dev

F# - Getting fst element of tuple from option type function

分類Dev

get an element from array with http GET php

分類Dev

How to remove an element from an array in Swift

分類Dev

get progress from dataTaskWithURL in swift

分類Dev

How can I return the last element number from a tuple that satisfies a condition?

分類Dev

Get element from which onclick function is called

分類Dev

How to get element in code behind from DataTemplate

分類Dev

Get Class Name of Element from MouseEvent Target

分類Dev

Get element from the selected item in the listView

分類Dev

Get nth element from numpy array

分類Dev

Using BeautifulSoup to get text from <pre> element

分類Dev

How to get position from top in pixel of an element?

分類Dev

Get table TD values from AngularJS $element

分類Dev

Javascript/ Jquery: Get element from string

分類Dev

Want to get a specific Element from a json array,

分類Dev

Get the distance from every element in a list to any other element

分類Dev

Extracting vectors from a tuple

分類Dev

Get path to Swift script from within script

分類Dev

Get Street Address From Contact Swift

分類Dev

How to get image from server in swift 4.1

分類Dev

Swift: Get variable in function from another function

分類Dev

How to get an Array of Objects from Firestore in Swift?

分類Dev

How to get data from UIDatePicker - Swift

Related 関連記事

  1. 1

    transform each element of tuple; get that tuple

  2. 2

    How we can get the Element from Set in Swift 2.2

  3. 3

    Get element.type from generic object in swift

  4. 4

    Sum the components of a tuple up by using std::get, std::tuple_size, std::tuple_element

  5. 5

    How to get something specific from a tuple

  6. 6

    How to return which element from the tuple matched in the endswith function

  7. 7

    F# - Getting fst element of tuple from option type function

  8. 8

    get an element from array with http GET php

  9. 9

    How to remove an element from an array in Swift

  10. 10

    get progress from dataTaskWithURL in swift

  11. 11

    How can I return the last element number from a tuple that satisfies a condition?

  12. 12

    Get element from which onclick function is called

  13. 13

    How to get element in code behind from DataTemplate

  14. 14

    Get Class Name of Element from MouseEvent Target

  15. 15

    Get element from the selected item in the listView

  16. 16

    Get nth element from numpy array

  17. 17

    Using BeautifulSoup to get text from <pre> element

  18. 18

    How to get position from top in pixel of an element?

  19. 19

    Get table TD values from AngularJS $element

  20. 20

    Javascript/ Jquery: Get element from string

  21. 21

    Want to get a specific Element from a json array,

  22. 22

    Get the distance from every element in a list to any other element

  23. 23

    Extracting vectors from a tuple

  24. 24

    Get path to Swift script from within script

  25. 25

    Get Street Address From Contact Swift

  26. 26

    How to get image from server in swift 4.1

  27. 27

    Swift: Get variable in function from another function

  28. 28

    How to get an Array of Objects from Firestore in Swift?

  29. 29

    How to get data from UIDatePicker - Swift

ホットタグ

アーカイブ