问题
Variable in a User class as a string. Using this variable as a key for a PFQuery and use it to filter out the users that don't have it.I need to skip users that don't have it and add the users that do to the VC display. Here is my code so far: In the VC where the variable is stored I have:
var genreSearch: NSString?
after viewDidLoad in a cell I have:
let genreSearch = table_data[indexPath.row]
userDefaults.setObject(genreSearch, forKey: "genre")
print(genreSearch)`
Then on the VC where the query is being called I have:
if let genreSearch = userDefaults.objectForKey("genre") as? [String] {
// do something here when a genresearch exists
let genreQuery = PFUser.query()
genreQuery!.whereKey("genre", containedIn: (genreSearch as [String]))
genreQuery!.findObjectsInBackgroundWithBlock { (users: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// success
for user in users! {
self.appUsers.append(user as! PFUser)
} // users
self.resultsPageTableView.reloadData()
} // error
else {
}
}
}
Any help would be greatly appreciated I have been stuck on this genreSearch query for what feels like weeks!!!
Also here is my code on VC2 that is updating the users info in a cell:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let singleCell: CustomCell = tableView.dequeueReusableCellWithIdentifier("mySingleCellid") as! CustomCell
let userObject = appUsers[indexPath.row] as PFObject
singleCell.userName.text = userObject["name"] as? String
So I have edited my code as I figured out the errors that were being called *I am able to open the VC but it does not filter out the results? It displays all the users instead of skipping the users that haven't got the chosen genre. Any ideas??? *
回答1:
If you are trying to make a query on the User table provided by parse, you should use instead of let genreQuery = PFQuery(className: "_User")
let genreQuery = PFUser.query()
回答2:
Finally figured out why it was not filtering out my users!!!
On the VC where the variable was stored I was missing a vital line above the class :
var genreSearch: String?
let userDefaults = NSUserDefaults.standardUserDefaults() //this one
Then in the ResultsVC I added:
if let genre = userDefaults.stringForKey("genre") //this line
{
print(genre)
let genreQuery = PFUser.query()
genreQuery?.whereKey("username",notEqualTo:PFUser.currentUser()!.username!)
genreQuery!.whereKey("genre", equalTo: genre)
genreQuery!.findObjectsInBackgroundWithBlock { (genres: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// success
self.appUsers.removeAll()
for genre in genres! {
self.appUsers.append(genre as! PFUser)
} // users
}
self.resultsPageTableView.reloadData()
self.refresher.endRefreshing()
} // error
I think it just wasn't saving or doing anything with the userDefaults on VC1 I must have missed a step!!!
来源:https://stackoverflow.com/questions/33595934/using-a-variable-as-a-key-for-a-pfquery-to-filter-out-users-swift-querying-a-us