【发布时间】:2015-07-10 13:20:48
【问题描述】:
如何将对象数组放入 theArray 以便在 tableView 中显示结果?由于查询是异步的,我不能简单地说 theArray = objects。那么,如何从查询中截取数组呢?
编辑:
Ashish 的回答解决了这个问题,但由于 Xcode 的建议,我不得不稍微更改一下代码。
现在,我试图打开它,因为我需要,
var new = object.objectId
println("first: \(new)")
if let tryToUnwrap = new as? NSString {
println("second: \(tryToUnwrap)")
}
我向下转换为 NSString 因为在 Swift 中字符串不是警告:从字符串转换?到不相关的类型'NSString'总是失败
代码有效,但它是什么意思?我怎么能摆脱这个?
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)")
}
}
【问题讨论】:
标签: arrays swift uitableview parse-platform