是否可以创建一个模仿CKRecord的类? [英] Is it possible to create a class that will imitate a CKRecord?

查看:49
本文介绍了是否可以创建一个模仿CKRecord的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在创建一个将大量重复使用某些记录的应用程序,因此我想到了一个方法:

So I'm creating an app that will be reusing some records a lot, so I came up with an idea to do that:

    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()是一个函数,该函数返回此cas中的所有类记录,并且可以正常工作):

later I created a function in tableViewCOntroler that is run when viewDidLoad is executed and when tableView is refreshed (getAllRecords() is a function that returns all the class records in this cas and it works):

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
}

现在的问题是我收到一条错误消息,内容为:

The problem is now that I got an error message that says:

严重错误:向下广播的Array元素与目标类型不匹配2016-12-23 22:44:38.681456 EdApp [4524:2186068]致命错误:向下广播的数组元素未能与目标类型匹配

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

我做错了什么?它甚至可以工作吗?谢谢!

What I did wrong? Can it even work? Thank You!

推荐答案

getAllRecords将返回一个CKRecords数组.它不会是Class类型的.(对于一个类,这是一个非常不好的名字)您不能仅将其强制转换为一个类.

The getAllRecords will return an array of CKRecords. It will not be of type Class. (Which is a very bad name for a class) You cannot just cast it to a Class.

您可以将其映射到您的班级,但这并不容易.您将需要从CKRecord获取已编码的SystemFields并使用编码器创建您的类.

You could map it to your Class, but it won't be easy. You would need to get the encodedSystemFields from the CKRecord and create your class using a coder.

我认为将CKRecord设置为您的Class的属性,并为您的Class创建一个接受CKRecord的初始化会更容易.然后,您可以将记录映射到具有以下内容的类:self.classes = records.map {Class(record:$ 0)}

I think it would be easier to make the CKRecord a property of your Class and create a init for your Class that accepts that CKRecord. You could then map your records to a Class with something like: self.classes = records.map { Class(record: $0) }

我想请您看看 https://github.com/evermeer/EVCloudKitDao 它具有一种将类自动转换为CKRecord并返回的机制.它将消除用于获取和设置属性的许多管道代码.

Can I sugest you have a look at https://github.com/evermeer/EVCloudKitDao It has a mechanism to automatically convert a class to a CKRecord and back. It will eliminate a lot of your plumbing code for getting and setting the properties.

这篇关于是否可以创建一个模仿CKRecord的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆