【发布时间】:2015-05-26 16:25:21
【问题描述】:
我在 Swift 中有一个 AnyObject 对象数组(最终,这个数组将通过查询 Parse 数据库来填充)。数组中的每个对象都具有出版物的属性,例如 fullTitle、url 和 journal。如何过滤数组以选择与任何值的搜索字符串匹配的所有对象(例如,fullTitle、url、或期刊包括“福布斯”)?
以下是来自游乐场的示例代码。首先是样本数组:
var publication1 = [
"fullTitle": "My first blog",
"url": "www.forbes.com/post1",
"journal": "Forbes
]
var publication2 = [
"fullTitle": "My second blog",
"url": "www.wsj.com/post1",
"journal": "Wall Street Journal"
]
var publications: [AnyObject] = [publication1, publication2]
那么,过滤函数:
func filterContentForSearchText(searchText: String) {
let filteredPublications = publications.filter() {
if let fullTitle = ($0)["fullTitle"] as? String {
return fullTitle.rangeOfString(searchText) != nil
} else {
return false
}
}
}
现在,如果我以“first”作为参数调用函数,它应该从数组中返回第一个对象:
println(filterContentForSearchText("first"))
但是,此命令没有结果。我怎样才能解决这个问题?另外,我如何查询对象的 all 字段以获取 searchText,而不仅仅是 fullTitle 字段?
谢谢。
【问题讨论】:
标签: arrays swift filter parse-platform