Stack Overflow Asked by protodimbo on September 25, 2020
I have a few arrays like this but much bigger:
let katakana = ["フシギダネ","フシギソウ","フシギバナ"]
let romadzi = ["Fushigidane","Fushigisou","Fushigibana"]
let number = ["001","002","003"]
And I want to print in one line all data from every array with similar index:
print("Hi im (katakana[0]), or (romadzi[0]) and my nymber is (number[0])")
print("Hi im (katakana[1]), or (romadzi[1]) and my nymber is (number[1])")
How to do that with my huge arrays?
Try
katakana.indices.forEach {
print("Hi im (katakana[$0]), or (romadzi[$0]) and my nymber is (number[$0])")
}
Given that all arrays are of same size
Correct answer by Sh_Khan on September 25, 2020
let katakana = ["フシギダネ","フシギソウ","フシギバナ"]
let romadzi = ["Fushigidane","Fushigisou","Fushigibana"]
let number = ["001","002","003"]
for i in 0..<katakana.count
{
print("Hi im (katakana[i]), or (romadzi[i]) and my nymber is (number[i])")
}
Answered by Jins George on September 25, 2020
A more efficient and reliable way is to create a custom struct. Each instance contains all information about one person.
The description
property provides to get the description string with array[i]
struct Person : CustomStringConvertible {
let katakana, romadzi, number : String
var description : String {
return "Hi I'm (katakana), or (romadzi) and my number is (number)"
}
}
let people = [Person(katakana: "フシギダネ", romadzi: "Fushigidane", number: "001"),
Person(katakana: "フシギソウ", romadzi: "Fushigisou", number: "002"),
Person(katakana: "フシギバナ", romadzi: "Fushigibana", number: "003")]
print(people[1]) // Hi I'm フシギソウ, or Fushigisou and my number is 002
and to print all
people.forEach{print($0)}
Answered by vadian on September 25, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP