通过对象

时间:2016-09-20 20:34:24

标签: ios arrays swift filter

所以我需要通过对象中包含的数组来过滤对象数组

对象看起来像这样

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}}

1 个答案:

答案 0 :(得分: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
相关问题