所以我创建一个应用程序,将被再利用一些记录了很多,所以我想出了一个主意,这样做:是否可以创建一个模仿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转换成我创建的类(它会加快速度),但它也可以。 –