As of April 12th, you must go to Progress SupportLink to create new support cases or to access existing cases. Please, bookmark the SupportLink URL and use the new portal to contact the support team.
Error: does not implement 'hostToKinveyPropertyMapping'
S
Sam Win
started a topic
over 7 years ago
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
P
Pranav J
said
over 7 years ago
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!
I have another problem with the image not loading and have started a new thread on it.
Thanks for the help.
Sam
P
Pranav J
said
over 7 years ago
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!
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.
Sam Win
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
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
- Oldest First
- Popular
- Newest First
Sorted by Newest FirstSam Win
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
Pranav J
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
Edward LoPinto
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.
-
Why do I get "Undefined symbols" errors when building with KinveyKit?
-
How do I register push tokens?
-
When using social login, to perform a log-out, do I need to log out of the social network, Kinvey, o
-
How can I assign additional properties to users?
-
Does KinveyKit support 64-bit ARM devices, such as iPhone 5s?
-
Authorization Token Invalid or Expired
-
BOOL and how it is stored in the database.
-
Offline saving throwing errors
-
Custom endpoint not able to form request object
-
Security through business logic
See all 437 topics