Reputation: 1943
How can I put objects array inside theArray in order to display results in tableView? since the query is asynchronous, I can't simply say theArray = objects. So, how can I intercept the array out from the query?
EDIT:
Ashish answer solved the problem, but I had to change the code a bit due to Xcode suggests.
now, I tried to unwrap this because I needed to,
var new = object.objectId
println("first: \(new)")
if let tryToUnwrap = new as? NSString {
println("second: \(tryToUnwrap)")
}
I downCast to NSString because in Swift string is not a warning: cast from String? to unrelated type'NSString' always fails
code works, but what does it means? how could I get rid of this?
import UIKit
import Parse
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var myTableView: UITableView!
var theArray : [PFObject] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//MARK: PFQUERY
//***********************************************************************
var query = PFQuery(className:"TestObject")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
self.theArray = (objects as? [PFObject])!
if self.theArray.count > 0 {
self.myTableView.reloadData()
}
if let objects = objects as? [PFObject] {
for object in objects {
// var new = object.objectId
// println("first: \(new)")
//
// if let tryToUnwrap = new as? NSString {
// println("second: \(tryToUnwrap)")
// }
println("here the object id: \(object.objectId)")
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
//***********************************************************************
// if let string = dict.objectForKey("SomeKey") as? String {
// // If I get here, I know that "SomeKey" is a valid key in the dictionary, I correctly
// // identified the type as String, and the value is now unwrapped and ready to use. In
// // this case "string" has the type "String".
// println(string)
// }
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: UITableViewDataSource (required to conform to UITableViewDataSource)
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return theArray.count
}
//MARK: UITableViewDataSource (required to conform to UITableViewDataSource)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell : TaskCell = tableView.dequeueReusableCellWithIdentifier("myCell") as! TaskCell
cell.objectIdLabel.text = theArray[indexPath.row].objectId
return cell
}
//MARK: UITableViewDelegate (not required)
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("section is: \(indexPath.section) row is: \(indexPath.row)")
}
}
Upvotes: 1
Views: 560
Reputation: 577
for your edit would you want the code:
var new = object.objectId
println("first: \(new)")
if let tryToUnwrap:String = new {
println("second: \(tryToUnwrap)")
}
Upvotes: 1
Reputation: 577
Parse also has it's own viewcontroller PFQueryTableViewController which includes a query that then sets up the cells in your table view. I think this may be useful for your code.
see https://parse.com/docs/ios/guide#user-interface-pfquerytableviewcontroller for more information.
Upvotes: 1
Reputation: 23902
Just reload the table after getting it.
var query = PFQuery(className:"TestObject")
// query.whereKey("playerName", equalTo:"Sean Plott")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
theArray = objects as? [PFObject]
if theArray.count > 0 {
myTableView.reloadData()
}
if let objects = objects as? [PFObject] {
for object in objects {
println(object.objectId)
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
Upvotes: 2