当我向 Kinvey 发送数据时出现 SIGABRT 错误

SIGABRT Error When I Sending Data to 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 class 当前是 class 中的嵌套 class =12=],并且您将 hostToKinveyPropertyMapping 实现为 ReservationViewController.

的方法

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

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.
        ]
    }
}

并从您的视图控制器中删除该代码。另外,确保你安全地解包所有可选值,并确认你的集合确实被命名为 "userReservation".