ROCloudModel 1.1.4

ROCloudModel 1.1.4

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2016年12月
SwiftSwift 版本3.0
SPM支持 SPM

Robin Oster 维护。



  • Robin Oster

ROCloudModel (beta)

在 CloudKit 上提供了一个抽象层,简化了 CKRecord 与 Swift 数据类之间的映射。

安装

ROCloudModel 可通过 CocoaPods 获取。要安装它,只需将以下行添加到 Podfile:

pod "ROCloudModel"

如何使用

使用 ROCloudModel 类作为您数据类的基本类。数据的映射直接在定义的属性的 getter/setter 方法中完成。

报告

class Report : ROCloudModel {

    required init() {
        super.init()
        self.recordType = "Reports"
        super.initializeRecord()
    }

    convenience init(name:String, title:String) {
        self.init()

        self.name = name
        self.title = title
    }

    var name:String {
        get {
            return self.record?["name"] as? String ?? ""
        }

        set(value) {
            self.record?["name"] = value
        }
    }

    var title:String {
        get {
            return self.record?["title"] as? String ?? ""
        }

        set(value) {
            self.record?["title"] = value
        }
    }
}

发表

class Post : ROCloudModel {

    required init() {
        super.init()
        self.recordType = "Posts"

        super.initializeRecord()
    }

    convenience init(title:String, report:Report? = nil) {
        self.init()

        self.title = title
        self.report = report
    }

    var title:String {
        get {
            return self.record?["title"] as? String ?? ""
        }

        set(value) {
            self.record?["title"] = value
        }
    }

    var report:Report? {
        get {
            return fetchReferenceSynchronous("report")
        }

        set(value) {
            self.setReferenceValue("report", value: value)
        }
    }

    // Asynch Reference
    func report(callback:(report:Report) -> ()) {
        fetchReference("report") { (report:Report) -> () in
            callback(report:report)
        }
    }

    // Asynch Reference List
    func reports(callback:(reports:Array<Report>) -> ()) {
        fetchReferenceArray("reports") { (reports:Array<Report>) -> () in
            callback(reports:reports)
        }
    }
}

通过 Webservice 获取数据

var postWebservice = ROCloudBaseWebservice<Post>()

self.postWebservice.load { (data) -> () in
    for post in data {
        SynchronizedLogger.sharedInstance.log("\(post.report?.title)")
        SynchronizedLogger.sharedInstance.log("\(post.title)")
    }
}

许可证

The MIT License (MIT)

Copyright (c) 2016 Robin Oster (http://prine.ch)

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

作者

Robin Oster, [email protected]