Reputation: 417
I have 21 arrays, divided by the alphabet (artistaA, artistaB etc) for the artist, and 21 for the title. I would like to build a unique array based on a structure named VociMontagna but I'm getting an error. My target is to crate this big array and then generate a TableView based on this one in order to use the filter. I have more than 800 titles so I prefer to keep the array separated.
Here the code:
func creaListaCanzoni(){
var tot = 0
var a = 0
for _ in stride(from: 0, to: titoliA.count , by: 1) {
let voce = VociMontagna(titolo: titoliA[a], artista: artistaA[a])
listaCanzoni.append(voce)
a = a + 1
tot = tot + 1
print("Tot: \(a)--Nome: \(listaCanzoni[a].nomeTesto)")
}
}
here the structure:
import UIKit
struct VociMontagna {
let titolo : String
let artista : String
}
Upvotes: 0
Views: 25
Reputation: 299345
First, it's unclear why you're keeping this data split up. 800 records is pretty small. Maintaining over 40 arrays seems very complicated.
Your mistake is that you're counting too high. The last index is titoliA.count - 1
. The indexes start at zero. Instead of this line:
for _ in stride(from: 0, to: titoliA.count , by: 1) {
You mean this:
for a in titolaA.indices {
The whole function would look like this:
func creaListaCanzoni(){
for a in titolaA.indices {
let voce = VociMontagna(titolo: titoliA[a], artista: artistaA[a])
listaCanzoni.append(voce)
print("Tot: \(a)--Nome: \(listaCanzoni[a].nomeTesto)")
}
}
That said, if you do need to merge two arrays (and I recommend you don't; just store them as a single array of structs), the better tool would be this:
let listaCanzoni = zip(titoliA, artistaA).map(VociMontagna.init)
This combines the two arrays and makes structs out them in a single step.
Upvotes: 1