我有一个名为“eposodeinfodata”的属性类

class EpisodeInfoData {
    var episodeId = ""
    var episodeName = ""
}

我已经将这些类的对象存储到字典中,字典的第一个元素是“String”,另一个元素是“eposodeinfodata”类对象的数组
var episodes    = Dictionary<String, [EpisodeInfoData]>()

let infoObj1 = EpisodeInfoData()
infoObj1.episodeId           = "1"
infoObj1.episodeName         = "one"

let infoObj2 = EpisodeInfoData()
infoObj2.episodeId           = "2"
infoObj2.episodeName         = "two"

let infoObj3 = EpisodeInfoData()
infoObj3.episodeId           = "3"
infoObj3.episodeName         = "three"

let infoObj4 = EpisodeInfoData()
infoObj4.episodeId           = "4"
infoObj4.episodeName         = "four"

let infoObj5 = EpisodeInfoData()
infoObj5.episodeId           = "5"
infoObj5.episodeName         = "five"

let infoObj6 = EpisodeInfoData()
infoObj6.episodeId           = "6"
infoObj6.episodeName         = "six"

let infoObj7 = EpisodeInfoData()
infoObj7.episodeId           = "7"
infoObj7.episodeName         = "seven"

let infoObj8 = EpisodeInfoData()
infoObj8.episodeId           = "8"
infoObj8.episodeName         = "eight"

episodes["season1"] = [infoObj1,infoObj2,infoObj3,infoObj4]
episodes["season2"] = [infoObj5,infoObj6,infoObj7,infoObj8]

我想在“情节”字典里找到季节的名字,它的书名是“八”。我试着用过滤镜了它不工作了。
有人能给我建议什么吗?不用过滤器就用过滤器找到结果。
提前谢谢你

最佳答案

不得不加上另一个答案,感谢那些在没有真正帮助的情况下投了反对票的人。
试试这个

if let episode = (episodes.filter { $0.1.contains { $0.episodeName == "eight" } }).first {
    print(episode.0) // season2
}

关于ios - 在对象数组字典中应用过滤器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39826104/

10-13 07:52