database - 向 Kinvey 发送数据时出现 SIGABRT 错误

标签 database swift kinvey

为什么我的数据没有发送到Kinvey后端?我的应用程序不断崩溃,并且收到 SIGABRT 错误。我尝试将 String 更改为 NSString 但这没有帮助。由于某种原因,我在 Kinvey 中的仪表板确实显示了 API 调用和数据存储,但它没有存储任何内容(我是初学者)。

import UIKit

class ReservationViewController: UIViewController {

    @IBOutlet weak var userName: UITextField!
    @IBOutlet weak var userEmail: UITextField!
    @IBOutlet weak var userSeat: UITextField!
    @IBOutlet weak var userDateTime: UIDatePicker!

    @IBAction func sendReservation(sender: AnyObject) {
        // STORE DATA
        let store = KCSAppdataStore.storeWithOptions([
            KCSStoreKeyCollectionName : "userReservation",
            KCSStoreKeyCollectionTemplateClass : Booking.self
        ])
        let reservation = Booking()
        reservation.name = userName.text
        reservation.email = userEmail.text
        reservation.seats = userSeat.text
        reservation.date = NSDate(timeIntervalSince1970: 1352149171) //sample date
        store.saveObject(
            reservation,
            withCompletionBlock: { (objectsOrNil: [AnyObject]!, errorOrNil: NSError!) -> Void in
                if errorOrNil != nil {
                    //save failed
                    NSLog("Save failed, with error: %@", errorOrNil.localizedFailureReason!)
                } else {
                    //save was successful
                    NSLog("Successfully saved event (id='%@').", (objectsOrNil[0] as! NSObject).kinveyObjectId())
                }
            },
            withProgressBlock: nil
        )
    }

    class Booking : NSObject {    //all NSObjects in Kinvey implicitly implement KCSPersistable
        var entityId: String? //Kinvey entity _id
        var name: NSString?
        var email: NSString?
        var seats: NSString?
        var date: NSDate?

    }

    override func hostToKinveyPropertyMapping() -> [NSObject : AnyObject]! {
        return [
            "entityId" : KCSEntityKeyId, //the required _id field
            "name" : "name",
            "email" : "email",
            "seats" : "seats",
            "date" : "date",
        ]
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

最佳答案

如果这实际上是您的代码,而不仅仅是您在复制粘贴时犯的错误,则问题在于您的 Booking 类当前是 ReservationViewController 中的嵌套类,并且您将 hostToKinveyPropertyMapping 实现为 ReservationViewController 的方法。

相反,为您的 Kinvey 类创建一个新文件:

class Booking: NSObject {
    var entityId: String? //Kinvey entity _id
    var name: NSString?
    var email: NSString?
    var seats: NSString?
    var date: NSDate?

    override func hostToKinveyPropertyMapping() -> [NSObject : AnyObject]! {
        return [
            "entityId" : KCSEntityKeyId, //the required _id field
            "name": "name",
            "email": "email",
            "seats": "seats",
            "date": "date"  //You also had an extra comma after the last item of this dictionary.
        ]
    }
}

并从 View Controller 中删除该代码。另外,请确保安全地解开所有可选值,并确认您的集合确实名为“userReservation”。

关于database - 向 Kinvey 发送数据时出现 SIGABRT 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36576835/

相关文章:

mysql - Talend 7.1 与 MySQL Server 8.0 (localhost) 连接失败

ios - 通过 subview 移动一个正方形

ios - Swift BigInt 到 [UInt8]

ios - 切换到 swift 2.0 并收到 AnyObject 错误

javascript - 如何通过 REST 使用 Kinvey 执行 'Hello World'?

android - 无法根据链接接收对象

sql - 限制非唯一值的返回

database - 甲骨文数据库 docker

ios - 将照片属性添加到 Kinvey 中的用户集合

mysql - 从数据库中的 tag-post 表检索标签名称