![](/img/trans.png)
[英]Simple Parse.com with findObjectsInBackgroundWithBlock does not return
[英]Parse findObjectsInBackgroundWithBlock return nil
这是我的代码迅速
class UserViewController: UITableViewController {
var userArray: NSMutableArray = []
@IBOutlet weak var friendListTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
retrieveMessages()
}
func retrieveMessages() {
var query = PFUser.query()
if let username = PFUser.currentUser().username {
query.whereKey("username", equalTo: username)
query.findObjectsInBackgroundWithBlock {
(objects, error) -> Void in
for object in objects! {
let usernames:String? = (object as PFObject)["Friends"] as? String
println(usernames)
if usernames != nil {
self.userArray.addObject(usernames!)
}
}
dispatch_async(dispatch_get_main_queue()) {
self.friendListTableView.reloadData()
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return userArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Update - replace as with as!
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = userArray[indexPath.row] as? String
return cell
}
问题是findObjectsInBackgroundWithBlock方法返回nil,所以我的表不会更新,该对象包含当前用户拥有的朋友数组。 那么我该如何解决呢? 在此先感谢您,如果需要更多信息,请告诉我!
更新:我将代码更改为(我正在使用Xcode 6.2)
class UserTableViewController: UITableViewController {
var userArray: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
retrieveMessages()
}
func retrieveMessages() {
var query = PFUser.query()!
if let username = PFUser.currentUser()!.username {
query.whereKey("username", equalTo: username)
query.findObjectsInBackgroundWithBlock { [weak self]
(objects, error) -> Void in
println(objects)
for object in objects! {
let username:String? = (object as PFUser).username
if username != nil {
self!.userArray.append(username!)
}
}
self!.tableView.reloadData()
}
}
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("userCell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = userArray[indexPath.row]
return cell
}
和我的应用程序崩溃,这是错误日志
2015-06-08 01:41:32.944 ParseStarterProject [635:14183] Interface Builder文件中的未知类_TtC19ParseStarterProject18UserViewController。 2015-06-08 01:41:32.966 ParseStarterProject [635:14183] *由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[setValue:forUndefinedKey:]:此类不适用于键friendListTableView的键值编码。” *第一个抛出调用堆栈:(0 CoreFoundation 0x000000010d35fa75 exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010efeabb7 objc_exception_throw + 45 2 CoreFoundation 0x000000010d35f6b9-[NSException提高] + 9 3 Foundation 0x000000010d803d43-[NSObject(NSKeyValueFording)set: CoreFoundation 0x000000010d2a95e0-[NSArray makeObjectsPerformSelector:] + 224 5 UIKit 0x000000010dfec4ed-[UINib InstantiateWithOwner:options:] + 1506 6 UIKit 0x000000010de4aa88-[UIViewController _loadViewFromNibNamed:bundle:] + 242 7 UIKit 0View0000000b-0b0000004b-0de0000000b-0b000000de4b-0b000000de4b-0b000000de4b-0b000000de4b-0b000000de4b-0b000000de4b-0b000000de4b -00b-0b-0b000000de4b-0b000000de4b-0b000000de4b-0b000000de4b-0b-0b000000de4b-0b-0b000000de4b-0b-0b-0b000000de4b-0b-0b000000de4b-0b-0b-0b-0b000000de4b-0b-0b000000de4b-0b-0b-0b000000de4b-0b-0b -00b----- [UITableViewController loadView] + 76 9 UIKit 0x000000010de4b2e9-[UIViewController loadViewIfRequired] + 75 10 UIKit 0x000000010de4b77e-[UIViewController视图] + 27 11 UIKit 0x000000010e3eef4e-[_ UIFullscreenPresentationController _setPresentedViewControllersInPredetedViewController 0:-PredeedtedViewr:Presentwith26DeUItr:26 -65 oller:] + 105 13 UIKit 0x000000010de57531-[UIViewController _presentViewController:withAnimationController:completion:] + 1746 14 UIKit 0x000000010de59871 __62- [UIViewController presentViewController:animated:completion:] _ block_invoke + 132 15 UIKit 0x000000mdededede 9595:[tionViewaniController] + 229 16 ParseStarterProject 0x000000010c81c1c0 _TFC19ParseStarterProject14ViewController13viewDidAppearfS0_FSbT_ + 1488 17 ParseStarterProject 0x000000010c81c231 _TToFC19ParseStarterProject14ViewController13viewDidAppearfS0_FSbT_ + 49 18的UIKit 0x000000010de4f361 - [UIViewController中_setViewAppearState:isAnimating:] + 567 19的UIKit 0x000000010de4feab - [UIViewController中_executeAfterAppearanceBlock] + 52 20的UIKit 0x000000010dd48adb _applyBlockToCFArrayCopiedToStack + 314 21的UIKit 0x000000010dd48974 _afterCACommitHandler + 547 22 CoreFoundation 0x000000010d294507 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23 23 CoreFoundation 0x000000010d294460 __CFRunLoopDoObservers + 368 24的CoreFoundation 0x000000010d28a293 __CFRunLoopRun + 1123 25的CoreFoundation 0x000000010d289bc6 CFRunLoopRunSpecific + 470 26 GraphicsServices 0x0000000110a99a58 GSEventRunModal + 161 27的UIKit 0x000000010dd25580 UIApplicationMain + 1282 28 ParseStarterProject 0x000000010c8203ae top_level_code + 78 29 ParseStarterProject 0x000000010c82048a主+ 42 30 libdyld.dylib 0x000000010f802145 START + 1)+的libc + abi.dylib:以类型为NSException(lldb)的未捕获异常终止
更新:修复错误,这些是我解析的类的屏幕快照,我的解析类为“用户” https://www.dropbox.com/s/6xp48v3yn0l2hje/Screen%20Shot%202015-06-03%20at%202.10.13%20PM。 png?dl = 0这是我当前用户在“朋友”列中的朋友列表https://www.dropbox.com/s/pd8mt8sf35u1m0v/Screen%20Shot%202015-06-03%20at%202.10.55%20PM.png ?dl = 0
PFUser.query仅查询User表。 看起来您真正要查询的是“ Friends”表或“ Messages”表。 不同的查询具有不同类型的对象以强制转换为对象,例如PFObject,PFUser ...如果要查询User表,则可以在下面找到工作代码:
import UIKit
import Parse
class UserTableViewController: UITableViewController {
var userArray = ["Jane","Ann"]
override func viewDidLoad() {
super.viewDidLoad()
retrieveMessages()
}
func retrieveMessages() {
var query = PFUser.query()!
if let username = PFUser.currentUser()!.username {
query.whereKey("username", equalTo: username)
query.findObjectsInBackgroundWithBlock { [weak self]
(objects, error) -> Void in
println(objects)
for object in objects! {
let username:String? = (object as! PFUser).username
if username != nil {
self!.userArray.append(username!)
}
}
self!.tableView.reloadData()
}
}
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("userCell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = userArray[indexPath.row]
return cell
}
}
如果要查询其他表,则必须将PFUser.query更改为PFQuery。 我最近遇到了同样的问题。 您可以在这里找到我编写的示例代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.