我想搜索名稱鍵的練習詞典,然後在表格視圖中顯示過濾結果。我使用這個功能過濾數組並在表視圖中顯示?
變量:
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