Swift: Get an Element from a Tuple

Swift: Get an element from a tuple

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

Swift: Get an array of element from an array of tuples

That's simple:

answers.map { $0.number }

How to find the index of a tuple element from an tuple array? iOS, Swift

Tuples can be compared for equality (as of Swift 2.2/Xcode 7.3.1), but
they do not conform to the Equatable protocol. Therefore you have
to use the predicate-based variant of indexOf to locate a tuple
in an array. Example:

let valueArray = [("a", "b"), ("c", "d")]
let tuple = ("c", "d")
if let index = valueArray.indexOf({ $0 == tuple }) {
print("found at index", index)
}

In Swift 4 the method has been renamed to firstIndex(where:):

if let index = valueArray.firstIndex(where: { $0 == tuple }) {
print("found at index", index)
}

How to get all the elements from array of tuples matching String?

You can go like this.

var contactsname = [(String,String)]()//firstname,lastname
contactsname = [("alex","joe"),("catty","drling"),("alex","fox"),("asta","alex")]
let key = "alex"

If you want to exact match the search name either with First name or Last name

let filterArray = contactsname.filter { $0.0 == key || $0.1 == key }

If you want to check First name and Last name contains specific string for that you can use contains

let filterArray = contactsname.filter { $0.0.contains(key) || $0.1.contains(key) }

Swift: Accessing members of tuples in Array

You've declared your Array as containing Any type. If you declare it like this the error should go away:

var array:[(name: String, surame: String)] = [alican]

If the array needs to be able to contain Any type you can pull out just those matching a particular type using flatMap.

var array:[Any] = [alican]
var nameSurnames = array.flatMap({ return $0 as? (name: String, surame: String) })
print(nameSurnames[0].name)

Swift - Search and Replace value in Tuple

You can search an array of tuples using firstIndex(where:) to find the index of the first element to match your condition. In your case, you are looking to match a specific id, so you would use a closure such as { $0.id == "2" }.

Once you find that index, you can use it to update the tuple inside of the array.

Example:

var info: [(id: String, name: String, years: Int, city: String)] = [
("1", "Fred", 35, "Bedrock"),
("2", "Wilma", 32, "Bedrock")
]

if let idx = info.firstIndex(where: { $0.id == "2" }) {
info[idx].city = "Boulder"
}

print(info)

Output

[(id: "1", name: "Fred", years: 35, city: "Bedrock"),
(id: "2", name: "Wilma", years: 32, city: "Boulder")]

Use a struct instead of a tuple

That said, you should really use a struct here to contain your values. Tuples in Swift are really meant for temporary use (such as returning multiple values from a function), so defining a struct to hold your values is preferred:

struct Record {
var id: String
var name: String
var years: Int
var city: String
}

var info: [Record] = [
.init(id: "1", name: "Fred", years: 35, city: "Bedrock"),
.init(id: "2", name: "Wilma", years: 32, city: "Bedrock")
]

The searching code would remain the same.



Related Topics



Leave a reply



Submit