如何从解析服务器检索数据?

时间:2015-05-20 16:54:04

标签: ios swift parse-platform parse-server swift-playground

我正在尝试制作一个聊天应用,所以我几乎完成了我的工作。但我不知道如何将我的数据从解析到我的代码。我尝试了一次,但它没有工作,当键入" query.findObjectsInBackgroundWithBlock {(objects:[AnyObject] !, error:NSError!) - >时出现错误。无效的"我想知道我将如何解决这个问题。

    @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
    }

}

3 个答案:

答案 0 :(得分:2)

Parse documentation正在以这种方式获取数据:

  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

答案 1 :(得分:0)

您需要使用[PFObject]?代替[AnyObject]!

答案 2 :(得分:0)

针对 Swift 3

进行了更新
  

如何从解析中检索数据?

如果您想使用findObjectsInBackgroundWithBlock

例如,你可以这样写:

query.findObjectsInBackground { (objects, error) in
    if let objects = objects {
        for object in objects {
            print(object.objectId)
        }
    } else {
        NSLog("Error: \(error!)")
    }
}