为什么我的数据没有发送到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实现为。

相反,为您的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.
        ]
    }
}


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

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

10-12 03:50