我是 swift 的新手,我在尝试过滤 UICollectionView 中的某些类别时遇到了一些问题。
这是我的代码,用于获取所有类别的所有文章。
func getArticlesforCategory(category: String) -> Int {
var result : Int = 0
for article in self.allArticles {
if category == article.category {
result += 1
}
}
return result
}
如何只过滤一个类别,例如“测试”?
我通过解析wordpress网站的xml得到所有的分类
最佳答案
您可以使用filter
函数来过滤您的数组:
func getArticlesforCategory(category: String) -> Int {
let filteredArray = allArticles.filter( {$0.category == category }) // Here you have filtered array
return filteredArray.count // If you want number of items pass count of filtered array
}
关于ios - 如何快速过滤 UICollectionView 中的单个类别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50484288/