我想搜索名称键的练习词典,然后在表格视图中显示过滤结果。我使用这个功能过滤数组并在表视图中显示?
变量:
var exercises = [Exercise]()
var filtered: [NSMutableArray] = []
var searchActive: Bool = false
在搜索功能我得到的错误
Type'Exercise”无标会员
和那么我有问题,结果是一个NSMutableArray,所以我不能设置resul牛逼的名字为单元格文本显示
无法将类型“的NSMutableArray”的价值在强制输入“串”
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
if (searchActive){
cell.textLabel?.text = filtered[indexPath.row] as String
} else {
let exercise = exercises[indexPath.row]
cell.textLabel!.text = exercise.name
}
return cell
}
这里是我运动的字典以供参考:
final public class Exercise {
var id: Int
var descrip: String
var name: String
var muscles: [Int]
var equipment: [Int]
public init?(dictionary: [String: Any]) {
guard
let id = dictionary["id"] as? Int,
let descrip = dictionary["description"] as? String,
let name = dictionary["name"] as? String,
let muscles = dictionary["muscles"] as? [Int],
let equipment = dictionary["equipment"] as? [Int]
else { return nil }
self.id = id
self.descrip = descrip
self.name = name
self.muscles = muscles
self.equipment = equipment
}
我可以通过使var过滤来修复第二个错误:[String] = []所以它可以用作单元格标题,但是这并不能解决第一个错误,我不知道是否正确的方式去解决它?
感谢这个,但是如果我设置过滤,也= [运动]()怎么了我的意思将它映射到单元名称时,它不再是一个字符串?看到我的代码部分cell.textLabel?.text = filtered [indexPath.row] as String – infernouk
@infernouk检查编辑答案:) –
我试过这个,它不喜欢它,我是否也需要编辑变量或者只是插入规定的代码?错误无法将类型'String'的值转换为结束结果类型'Exercise'并在表格单元格名称语句中:无法将强制类型'Exercise'的值转换为键入'String' – infernouk