这是我在swift中的代码
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 raise] + 9 3基础0x000000010d803d43 - [NSObject(NSKeyValueCoding)setValue:forKey:] + 259 4 CoreFoundation 0x000000010d2a95e0 - [NSArray makeObjectsPerformSelector:] + 224 5 UIKit 0x000000010dfec4ed - [UINib instantiateWithOwner:options:] + 1506 6 UIKit 0x000000010de4aa88 - [UIViewController _loadViewFromNibNamed:bundle:] + 242 7 UIKit 0x000000010de4b078 - [UIViewController loadView] + 109 8 UIKit 0x000000010e008bd8 - [UITableViewController loadView] + 76 9 UIKit 0x000000010de4b2e9 - [UIViewController loadViewIfRequired] + 75 10 UIKit 0x000000010de4b77e - [UIViewController视图] + 27 11 UIKit 0x000000010e3eef4e - [_ UIFullscreenPresentationController _setPresentedViewController:] + 65 12 UIKit 0x000000010de26d69 - [UIPresentationController initWithPresentedViewController:presentsViewController:] + 105 13 UIKit 0x000000010de57531 - [UIViewController _presentViewController:withAnimationController:completion:] + 1746 14 UIKit 0x000000010de59871 __62- [UIViewController presentViewController:animated:completion:] _ block_invoke + 132 15 UIKit 0x000000010de59795 - [UIViewController presentViewController:animated:completion:] + 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 main + 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 这是我目前用户的朋友列表" Friends"柱 https://www.dropbox.com/s/pd8mt8sf35u1m0v/Screen%20Shot%202015-06-03%20at%202.10.55%20PM.png?dl=0
答案 0 :(得分:0)
PFUser.query仅查询User表。看起来你真正想要查询的是“朋友”表或“消息”表。不同的查询具有要投射到的不同类型的对象,例如, PFObject,PFUser ...如果你想查询用户表,你可以找到下面的工作代码:
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。我最近遇到了同样的问题。你可以找到我写的here
示例代码