Reputation: 176
I have a Product
class in Parse
, and I'm trying to identify the user and their email associated with a product when selected. In my Product
class, I do have a user pointer.
Here is what I have so far:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let product: PFObject = PFObject(className: "Product")
let createdBy: AnyObject! = product.objectForKey("user")?.email
println("You selected cell #\(indexPath.row), created by \(createdBy)!")
The code just gives me "nil" for the createdBy
value.
I've tried following the Parse
docs, but I honestly don't know what I'm doing. This is my first app.
Upvotes: 2
Views: 55
Reputation: 8279
What you need to do is save a userID
per product object. In fact, every user already has a property called objectId
when you create a new parse user object. You can use this unique identifier to set to a property called userID
in every product object. Or if you would like it to be the user's email, make sure the email is stored to the user:
let user = PFUser()
// Fill in the rest here...
user.setObject("[email protected]", forKey: "email")
user.save()
And then when you create your purchase objects (make sure you have an instantiated user
below
let product = PFProduct()
// Fill in the rest here...
product.setObject(user["email"], forKey: "purchaserEmail")
product.save()
So then when you are trying to check for the email associated with the purchaser of that product
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let product: PFProduct = self.products.objectAtIndex(indexPath.row) as! PFProduct
var purchaserEmail: String = product["purchaserEmail"]
println("Go spam \(purchaserEmail) more.")
}
And you can do the same with the username of the user if you'd like to save that to the product object as well. Hope this helped!
Upvotes: 2
Reputation: 5258
I think in your query where you pull the information from Parse you need to use the includeKey
parameter.
It would look like this:
query.includeKey("user")
and that should allow you to get at that user info.
Upvotes: 1