Reputation: 79
Im trying to make a chat app so i almost finish my woking. But i don't know how to get my data from parse to my code back. I tried once but it isn't working there is an error coming when type "query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in" that i want to know how i'm gonna fix this issue.
@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
}
}
Upvotes: 0
Views: 423
Reputation: 5499
Updated for Swift 3
How to retrieve data from parse?
If you want to use findObjectsInBackgroundWithBlock
For example you can write this:
query.findObjectsInBackground { (objects, error) in
if let objects = objects {
for object in objects {
print(object.objectId)
}
} else {
NSLog("Error: \(error!)")
}
}
Upvotes: 0
Reputation: 261
Parse documentation is showing this way to get data:
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)")
}
}
The problem is that you are using objects: [AnyObject]!
instead of objects: [PFObject]?
Edit:
also you can write like this (objects, error) -> Void in
Upvotes: 2