所以我創建一個應用程序,將被再利用一些記錄了很多,所以我想出了一個主意,這樣做:是否可以創建一個模仿CKRecord的類?
import CloudKit
class Class: CKRecord {
override init(recordType: String = "Class") {
super.init(recordType: recordType)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var name: String? {
get {
return value(forKey: "name") as? String
} set {
setValue(newValue!, forKey: "name")
}
}
var classDescription: String? {
get {
return value(forKey: "description") as? String
} set {
setValue(newValue!, forKey: "description")
}
}
var posts: [CKReference]? {
get {
return value(forKey: "posts") as? [CKReference]
} set {
setValue(newValue!, forKey: "posts")
}
}
var users: [CKReference]? {
get {
return value(forKey: "users") as? [CKReference]
} set {
setValue(newValue!, forKey: "users")
}
}
}
後來我在tableViewCOntroler創建的函數執行viewDidLoad中時運行當的tableView刷新(getAllRecords()是返回這個中科院所有的類記錄的功能和它的作品):
func refresh() {
queue.addOperation {
self.database.getAllRecords(withRecordType: "Class", withDesiredKeys: ["posts", "name"], sortForkey: "name", ascending: false, withResultLimit: CKQueryOperationMaximumResults, operations: 1){ records, error in
if error == nil {
self.classes = records as! [Class]
OperationQueue.main.addOperation {
self.tableView.reloadData()
self.refreshControl?.endRefreshing()
}
}
}
}
}
然後我創建細胞:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "class", for: indexPath)
// Configure the cell...
cell.textLabel?.text = (classes[indexPath.row].name)!
return cell
}
現在的問題是,我得到了一個錯誤,指出消息:
fatal error: Down-casted Array element failed to match the target type 2016-12-23 22:44:38.681456 EdApp[4524:2186068] fatal error: Down-casted Array element failed to match the target type
我做錯了什麼?它可以工作嗎?謝謝!
謝謝你的回答!我認爲它很好地回答了我的問題。很不幸的是,我不能直接將CKRecord轉換成我創建的類(它會加快速度),但它也可以。 –