This is my code in 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
}
the problem is the findObjectsInBackgroundWithBlock method return nil so my table won't update,the object contains array of friends that current user had. So how do I fix this? thanks in advance and let me know if you need more information!
Update: I've change my code to (I'm using 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
}
and my app crash, this is the error log
2015-06-08 01:41:32.944 ParseStarterProject[635:14183] Unknown class _TtC19ParseStarterProject18UserViewController in Interface Builder file. 2015-06-08 01:41:32.966 ParseStarterProject[635:14183] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key friendListTableView.' * First throw call stack: ( 0 CoreFoundation 0x000000010d35fa75 exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010efeabb7 objc_exception_throw + 45 2 CoreFoundation 0x000000010d35f6b9 -[NSException raise] + 9 3 Foundation 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 view] + 27 11 UIKit 0x000000010e3eef4e -[_UIFullscreenPresentationController _setPresentedViewController:] + 65 12 UIKit 0x000000010de26d69 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 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: terminating with uncaught exception of type NSException (lldb)
Update: fix the error and these are screenshots of my parse my parse's class "User" https://www.dropbox.com/s/6xp48v3yn0l2hje/Screen%20Shot%202015-06-03%20at%202.10.13%20PM.png?dl=0 This is my current user's friend list inside "Friends" column https://www.dropbox.com/s/pd8mt8sf35u1m0v/Screen%20Shot%202015-06-03%20at%202.10.55%20PM.png?dl=0
println(usernames)
print? Did you inspect theerror
object? What does that say? How do you know the result shouldn't benil
? Does this user have any friends? – Aaron Brager