I have to sort an array of Custom class 'SongItem' based on another array which is my String array with multiple types (have to apply OR validation).
Suppose my Custom array is :
class SongItem: NSObject {
var songId: String?
var title: String?
var artistName: String?
var isrc: String?
var genreArray = [String]()
}
<SongItem: 0x1c03097e0>
<SongItem: 0x1c03097e0>
<SongItem: 0x1c03097e0>
<SongItem: 0x1c03097e0>
<SongItem: 0x1c03097e0>
<SongItem: 0x1c03097e0>
I have another String array that has three values:
genreStringArray = ["Music","Alternative","Blues"]
Now I have to filter my Custom array based on another String array.
Here is my code:
for i in 0..<genreStringArray.count {
let genre = genreStringArray[i]
self.customArray = self.customArray.filter({ $0.genreArray.contains(genre) })
}
As I am using for loop here so filtering is working fine in AND manner, but I have to filter this with OR manner. I mean this should be like :
self.datasource = self.datasource.filter { $0.genreArray.contains(genre1) || $0.genreArray.contains(genre2) || $0.genreArray.contains(genre3)}
Please anyone suggest me.
genre1,genre2andgenre3? maybe you should add some sample data (the print of th Custom array is not helpful) and expected outputgenreArraythe same asgenreStringArray? And isdatasourcethe same ascustom array?