Save item with Swift

Hello,
I am trying to make a question and answer type app to mimic Quora.
I have created the entities and have the persistance connection working and verified. When I try to create a new question from user input, I get this error.

[<BloQuery2.Question 0x1072937a0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key ___class.

I have looked all thru the documentation and online, cannot find reference to ___class.
When the user clicks the button, the app crashes. However, online, the question gets created, but without text.

Here is my code.

On AddQuestionViewController

 @IBAction func addQuestion(sender: UIButton) {
 
 backendless.setThrowException(false)
 
 var result : AnyObject?
 var fault : Fault?
 
 result = backendless.persistenceService.save(Question())
 if (result is Question) {
 let obj : AnyObject = backendless.persistenceService.findByObject(Question)
 if (obj is Question) {
 let obj1 = obj as! Question
 print("\nQuestion (1): \(obj1.description)")
 userQuestion.text = obj1.question
 
 }
 }
 if (result is Fault) {
 print("Something went wrong")
 }

and the QuestionEntity.swift file

import UIKit
import Foundation


class Question: NSObject {
 
 var question : String?
 var userName : String?
 var objectId: String?
 var created : NSDate?
 var owner:BackendlessUser?
 
 }

Hi Eric!

I can reproduce this issue. We will investigate this use case.
But I saw that after commenting line with …persistenceService.findByObject(Question)
app not crashes. I think problem was caused by this method.
Regards,
Kate.

Hi Eric,

PersistenceService ‘findByObject’ mehods are used only for external DB - you should use another ‘find…’ methods, foe example, ‘findById’ :

   
    func findByIdAsync() {
        
        let contact = Contact()
        contact.name = "Jack Daniels"
        contact.age = 147
        contact.phone = "777-777-777"
        contact.title = "Favorites"
        
        let dataStore = backendless.data.of(Contact.ofClass())
        
        dataStore.save(
            contact,
            response: { (result: AnyObject!) -> Void in
                let savedContact = result as! Contact
                print("Contact has been saved: \(savedContact.objectId)")
                
                dataStore.findID(
                    savedContact.objectId,
                    response: { (result: AnyObject!) -> Void in
                        let foundContact = result as! Contact
                        print("Contact has been found: \(foundContact.objectId)")
                    },
                    error: { (fault: Fault!) -> Void in
                        print("Server reported an error (2): \(fault)")
                })
            },
            error: { (fault: Fault!) -> Void in
                print("Server reported an error (1): \(fault)")
        })
    } 

I’ve created a sample for you - https://drive.google.com/open?id=0B3yyaWhA4ib0b05FWjd4U1dpUW8, it works fine for me, you can try it and compare with your project.

Thank you, this solved this problem.