I have create the following struct to display a tableview:
我创建了以下结构来显示tableview:
struct Section {
var heading : String
var items : [MusicModel]
init(category: String, objects : [MusicModel]) {
heading = category
items = objects
}
}
Then I have the following function to add objects to the array:
然后我有以下函数将对象添加到数组:
var sectionsArray = [Section]()
func getSectionsFromData() -> [Section] {
let basicStrokes = Section(category: "Basic Strokes",
objects: [MusicModel(title: Title.EightOnAHand, author: .None, snare: true, tenor: true)])
sectionsArray.append(basicStrokes)
return sectionsArray
}
So the tableview loads just fine with section header and rows for the data. I want to be able to filter the table cells but am having a tough time figuring out the proper syntax to filter an array of objects based on a user selected instrument that is sent to a filtering function. Here is what I have:
因此,tableview可以很好地加载数据的节头和行。我希望能够过滤表格单元格,但是很难确定基于用户选择的发送到过滤功能的工具来过滤对象数组的正确语法。这是我有的:
func getMusic(instrument: MusicData.Instrument) -> [Section] {
if instrument == .Tenor {
let filtered = sections.filter {$0.items.filter {$0.tenor == true}}
return filtered
} else {
let filtered = sections.filter {$0.items.filter {$0.snare == true}}
return filtered
}
}
The object is returned only if snare or tenor is true. The error that I am getting is: Cannot invoke 'filter' with an argument list of type '(@noescape (MusicModel) throws -> Bool)'
仅当snare或tenor为true时才返回该对象。我得到的错误是:无法使用类型'(@noescape(MusicModel)throws - > Bool)的参数列表调用'filter'
Any idea? If you have a suggestion on better way to do this I would greatly appreciate it. Thanks!
任何想法?如果您对更好的方法有建议我会非常感激。谢谢!
2 个解决方案
#1
2
Use this updated getMusic method,
使用此更新的getMusic方法,
func getMusic(instrument: MusicData.Instrument) -> [Section] {
if instrument == .Tenor {
var filtered = sections.filter {$0.items.filter {$0.tenor == true}.count > 0}
filtered = filtered.map({ (var section: Section) -> Section in
section.items = section.items.filter {$0.tenor == true}
return section
})
return filtered
} else {
let filtered = sections.filter {$0.items.filter {$0.snare == true}.count > 0}
filtered = filtered.map({ (var section: Section) -> Section in
section.items = section.items.filter {$0.snare == true}
return section
})
return filtered
}
}
#2
0
Or the following one. (Be warned that your Section is struct
, which means it'll be copied around.)
或者以下一个。 (请注意,您的Section是struct,这意味着它将被复制。)
func getMusic(instrument: MusicData.Instrument) -> [Section] {
return sections.map { (var s: Section) -> Section in
s.items = s.items.filter { instrument == .Tenor ? $0.tenor : $0.snare }
return s
}
}
#1
2
Use this updated getMusic method,
使用此更新的getMusic方法,
func getMusic(instrument: MusicData.Instrument) -> [Section] {
if instrument == .Tenor {
var filtered = sections.filter {$0.items.filter {$0.tenor == true}.count > 0}
filtered = filtered.map({ (var section: Section) -> Section in
section.items = section.items.filter {$0.tenor == true}
return section
})
return filtered
} else {
let filtered = sections.filter {$0.items.filter {$0.snare == true}.count > 0}
filtered = filtered.map({ (var section: Section) -> Section in
section.items = section.items.filter {$0.snare == true}
return section
})
return filtered
}
}
#2
0
Or the following one. (Be warned that your Section is struct
, which means it'll be copied around.)
或者以下一个。 (请注意,您的Section是struct,这意味着它将被复制。)
func getMusic(instrument: MusicData.Instrument) -> [Section] {
return sections.map { (var s: Section) -> Section in
s.items = s.items.filter { instrument == .Tenor ? $0.tenor : $0.snare }
return s
}
}