Start a new topic
Answered

Error: does not implement 'hostToKinveyPropertyMapping'

 Hi,


I have a Swift code to upload some String and an image to the Kinvey data backend. It compiles successfully and it connects to Kinvey with API calls. I was able to sign up new users and retrieve them back.


But when I try to save data to the data backend, the simulator produces an error:

does not implement 'hostToKinveyPropertyMapping', a required 'KCSPersistable' method for saving the object to the backend'


I have followed the guide and have the function override for hostToKinveyPropertyMapping as instructed. Where is the error? Thanks. The code is below.


Xcode 7.2.1

KinveyKit 1.40.7



 

import UIKit


class UserTableViewController: UITableViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
   
    var userArray: [String] = []
   
    var activeRecipient = 0
   
    class Image : NSObject {    //all NSObjects in Kinvey implicitly implement KCSPersistable
        var entityId: String! //Kinvey entity _id
        var image: UIImage!
        var senderUsername: String!
        var recipientUsername: String!
    }
   
    override func hostToKinveyPropertyMapping() -> [NSObject : AnyObject]! {
        return [
            "entityId" : KCSEntityKeyId, //the required _id field
            "image" : "image",
            "senderUsername" : "senderUsername",
            "recipientUsername" : "recipientUsername"
        ]
    }

   
   
    func pickImage(sender: AnyObject) {
       
        var image = UIImagePickerController()
        image.delegate = self
        image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        image.allowsEditing = false
       
        self.presentViewController(image, animated: true, completion: nil)
       
    }
   
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
       
        self.dismissViewControllerAnimated(true, completion: nil)
       
        // Upload to Kinvey
       
        var imageToSend = KCSAppdataStore.storeWithOptions([
            KCSStoreKeyCollectionName : "Images",
            KCSStoreKeyCollectionTemplateClass : Image.self
            ])
       
        let imageSendingData = Image()
        imageSendingData.image = UIImage(data: UIImageJPEGRepresentation(image, 0.5)!)
        imageSendingData.senderUsername = KCSUser.activeUser().username
        imageSendingData.recipientUsername = userArray[activeRecipient]
       
        imageToSend.saveObject(imageSendingData, withCompletionBlock: { (objectsOrNil, errorOrNil) -> Void in
           
            if errorOrNil != nil {
               
                print("Error saving image: \(errorOrNil)")
               
            } else {
               
                print("Save image successful")
               
            }
           
            }, withProgressBlock: nil)
       
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let userQuery = KCSAppdataStore.storeWithOptions([
            KCSStoreKeyCollectionName : KCSUserCollectionName,
            KCSStoreKeyCollectionTemplateClass : KCSUser.self
            ])  // Kinvey's syntax for declaring the user collection object. 'KCSUserCollectionName' is a reserved constant for the Users collection. KCSUSer is the class.
       
        userQuery.queryWithQuery(KCSQuery(onField: "_id", usingConditional: .KCSNotEqual, forValue: KCSUser.activeUser().kinveyObjectId()), withCompletionBlock: { (users, queryError) -> Void in
           
            for user in users {
                               
                self.userArray.append(user.username)
               
                self.tableView.reloadData()
               
            }
           
           
            }, withProgressBlock: nil)
       
       
    }

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

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return userArray.count
    }

   
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

        cell.textLabel?.text = userArray[indexPath.row]

        return cell
    }
   
   
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
       
        activeRecipient = indexPath.row
       
        pickImage(self)
       
    }

 

 

 




Best Answer

Sam,


In your case, "hostToKinveyPropertyMapping” method should be a part of class Image. Currently it is written as part of "class UserTableViewController".

If we change the closing braces of "class Image" after "hostToKinveyPropertyMapping" method, then it will become part of "class Image” and it should take care of your error..

Corrected code of Image Class :

    class Image : NSObject {    //all NSObjects in Kinvey implicitly implement KCSPersistable
        var entityId: String! //Kinvey entity _id
        var image: UIImage!
        var senderUsername: String!
        var recipientUsername: String!
   
   
        override func hostToKinveyPropertyMapping() -> [NSObject : AnyObject]! {
            return [
                "entityId" : KCSEntityKeyId, //the required _id field
                "image" : "image",
                "senderUsername" : "senderUsername",
                "recipientUsername" : "recipientUsername",
            ]
        }
   
    }



Thanks,

Pranav

Kinvey Support


Someone else on this forum has been having a similar problem. I think the problem is that you've nested the Image class inside of your UserTableViewController, and then you've implemented hostToKinveyPropertyMapping as a method of UserTableViewController rather than a method of the Image class.


Image should be it's own class, completely separate from UserTableViewController, and hostToKinveyPropertyMapping should be a method of the Image class.

Answer

Sam,


In your case, "hostToKinveyPropertyMapping” method should be a part of class Image. Currently it is written as part of "class UserTableViewController".

If we change the closing braces of "class Image" after "hostToKinveyPropertyMapping" method, then it will become part of "class Image” and it should take care of your error..

Corrected code of Image Class :

    class Image : NSObject {    //all NSObjects in Kinvey implicitly implement KCSPersistable
        var entityId: String! //Kinvey entity _id
        var image: UIImage!
        var senderUsername: String!
        var recipientUsername: String!
   
   
        override func hostToKinveyPropertyMapping() -> [NSObject : AnyObject]! {
            return [
                "entityId" : KCSEntityKeyId, //the required _id field
                "image" : "image",
                "senderUsername" : "senderUsername",
                "recipientUsername" : "recipientUsername",
            ]
        }
   
    }



Thanks,

Pranav

Kinvey Support

Hi Edward, Pranav,

Thank you! It does work!

I have another problem with the image not loading and have started a new thread on it.

Thanks for the help.

Sam

 

Login or Signup to post a comment