简体   繁体   中英

Why am I getting the pointer error?

In the user class I have a pointer "Friendship" to the user class, but when I run the program I get the error - 在此处输入图片说明

EDIT: I have figured out why I get the error. "Frinendship is not a pointer, it is a PFRelation, but I still want to access the ProPic and username. How would I do this?

Below is the user class 在此处输入图片说明

Below is the Pointer to the user class. 在此处输入图片说明

func getFriendPic(){

    let imagequery = PFQuery(className: "_User")
    imagequery.whereKey("username", equalTo: PFUser.currentUser()!)
    imagequery.includeKey("Friendship")
    imagequery.findObjectsInBackgroundWithBlock {( objects: [AnyObject]?, error: NSError?) -> Void in
        for object in objects!{
            let userPic = object["ProPic"] as! PFFile
            userPic.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
                if(error == nil){
                    let image = UIImage(data: imageData!)
                    self.arrayOfFriends.append(image!)
                    print(self.arrayOfFriends)

                }
               // dispatch_async(dispatch_get_main_queue()) {
               //     self.collectionView.reloadData()
               // }
            })

        }
    }
}

func getFriendName(){
    var query = PFQuery(className: "_User")
    query.whereKey("username", equalTo: PFUser.currentUser()!)
    query.includeKey("Friendship")
    query.findObjectsInBackgroundWithBlock({
        (objects: [AnyObject]?, error: NSError?) -> Void in
        var objectIDs = objects as! [PFObject]

        for i in 0...objectIDs.count-1{
            self.arrayOfFriendsNames.append(objectIDs[i].valueForKey("username") as! String)
            print(self.arrayOfFriendsNames)
        }



    })
}

The include query parameter doesn't work with Relation . Once you retrieved your user, you need to execute a second query to get the Friendship.

Here's an example to retrieve a PFRelation:

var relation = user.relationForKey("Friendship")
relation.query().findObjectsInBackgroundWithBlock {
  (objects: [PFObject]?, error: NSError?) -> Void in
  if let error = error {
    // There was an error
  } else {
    // objects contains the friendships of the user
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM