2016-09-20 112 views
0

所以我需要通過包含在對象的陣列來篩選對象的數組由包含在對象字典的陣列篩選對象的數組

對象看起來像這樣

class thing { 

let list = [stuff, stuff, stuff, stuff] 

} 

class stuff { 

var name = "ugly" 

} 

的陣列看起來像這樣

thingArray = [thing, thing, thing, thing] 

和到目前爲止我的代碼看起來像這樣

newArray = thingArray.filter { receipt in 

return thing.thingArray.lowercaseString.containsString(searchText.lowercaseString) 
     } 

所以我的問題是,我不能讓這個函數的工作,我真正需要的是一種方法來過濾對象數組中包含的對象的數組對象。任何幫助肯定是讚賞的。

p.s.我只是嘗試這樣做無濟於事:

test = thingArray.filter {$0.list.filter {$0.name.lowercaseString.containsString(searchText.lowercaseString) == true}} 

回答

2

如果我理解正確的話,你的問題可以用這樣的事情來解決:

class thing { 

init(list: [String]) { 
    self.list = list 
} 

var list: [String] 

} 

let thingArray = [thing(list: ["one", "two", "three"]), thing(list: ["uno", "due", "tre"]), thing(list: ["uno", "dos", "tres"])] 

let searchString = "uno" 

let result = thingArray.filter { 
    $0.list.contains(searchString) 
} 

//it will print "uno", "due", "tre" 
result.first?.list 
+0

如果你想搜索不區分大小寫,你可以這樣做: let result = thingArray.filter { ($ 0.list.filter { $ 0.lowercased()== searchString.lowercased() } .first!= nil) } – matsoftware

+0

工作完美。謝謝!!!! – WikipediaBrown

+0

@PCD請注意,如果性能問題,您可以通過使用'Set'而不是'Array'來加速這個問題。 – Alexander

相關問題