0

我正在尝试制作一个聊天应用程序,所以我几乎完成了我的工作。但我不知道如何将我的数据从解析中返回到我的代码中。我试过一次,但它不起作用当输入“query.findObjectsInBackgroundWithBlock {(对象:[AnyObject]!,错误:NSError!)-> Void in”时出现错误,我想知道我将如何修复这个问题。

    @IBOutlet var chatTextField: UITextField!

    @IBOutlet var DockHight: NSLayoutConstraint!

    @IBOutlet var SendButton: UIButton!

    @IBOutlet var messageTableview: UITableView!

    var messagesArray:[String] = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.
        self.messageTableview.delegate = self
        self.messageTableview.dataSource = self

        self.chatTextField.delegate = self

        let tappGesture:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tableViewTapped")
        self.messageTableview.addGestureRecognizer(tappGesture)

        //retrive


    }

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


    @IBAction func SendButton(sender: UIButton) {

        //send button is stoped

        //end edding methode for the text

        self.chatTextField.endEditing(true)

        self.chatTextField.enabled = false
        self.SendButton.enabled = false


        //create a PFobject

        var message = PFObject(className:"Message")
        message["Text"] = "\(chatTextField.text)"
        message.saveInBackgroundWithBlock {
            (success: Bool, error: NSError?) -> Void in
            if (success == true) {
                NSLog("The Message has been sent")
            } else {
                NSLog(error!.description)

            }

        self.SendButton.enabled = true
        self.chatTextField.enabled = true
        }
    }

    func retrieveMessage() {

        var query:PFQuery = PFQuery(className: "Message")
        query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in

        }
    }




    func tableViewTapped(){

        self.chatTextField.endEditing(true)
    }

    //textfield deligate methode
    func textFieldDidBeginEditing(textField: UITextField) {

        self.view.layoutIfNeeded()
        UIView.animateWithDuration(0.2, animations: {

            self.DockHight.constant = 350
            self.view.layoutIfNeeded()

            }, completion: nil)
    }


    func textFieldDidEndEditing(textField: UITextField) {

        self.view.layoutIfNeeded()
        UIView.animateWithDuration(0.3, animations: {

            self.DockHight.constant = 44
            self.view.layoutIfNeeded()

            }, completion: nil)
    }


    //makrk tableview delegate methode

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = self.messageTableview.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

        cell.textLabel?.text = self.messagesArray[indexPath.row]

        return cell
    }
    var window: UIWindow?

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return messagesArray.count
    }

}
4

3 回答 3

2

Parse 文档显示了这种获取数据的方式:

  query.findObjectsInBackgroundWithBlock {
  (objects: [PFObject]?, error: NSError?) -> Void in

      if error == nil {

         // Do something with the found objects
         if let objects = objects {
            for object in objects {
                print(object.objectId)
            }
         }

      } else {
         // Log details of the failure
         print("Error: \(error!) \(error!.userInfo)")
      }
  }

问题是您正在使用objects: [AnyObject]!而不是objects: [PFObject]?

编辑: 你也可以这样写(objects, error) -> Void in

于 2016-04-24T15:47:48.637 回答
0

Swift 3更新

如何从解析中检索数据?

如果你想使用findObjectsInBackgroundWithBlock

例如你可以这样写:

query.findObjectsInBackground { (objects, error) in
    if let objects = objects {
        for object in objects {
            print(object.objectId)
        }
    } else {
        NSLog("Error: \(error!)")
    }
}
于 2017-05-12T16:26:21.137 回答
0

您需要使用[PFObject]?而不是[AnyObject]!

于 2016-04-24T13:15:23.763 回答