gpt4 book ai didi

ios - PFUser 查询在第一次运行时不会更改变量

转载 作者:行者123 更新时间:2023-11-30 13:52:20 26 4
gpt4 key购买 nike

我有一个文本字段,用户可以在其中输入另一个用户的用户名,将他添加为 friend 。我使用 PFQuery 查询所有用户,然后检查在文本字段中输入的用户名是否存在。如果存在,则会出现一个按钮(我知道我不小心在文本中将其称为标签),如果用户按下该按钮,则应添加其他用户。我的查询有问题,当我搜索用户(我知道该用户存在)时,它不会在第一次运行时打印用户名,只有当我再次运行它时才打印用户名。似乎它只在第二次改变值。

import UIKit
import Parse
import Bolts

var userToAdd = [String]()

class AddFriendViewController: UIViewController {

@IBOutlet var addFriendLabel: UIButton!
@IBOutlet var searchUserTF: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
addFriendLabel.setTitle("", forState: .Normal)
// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func checkForUser() {

if searchUserTF.text != "" {

var username = searchUserTF.text
var query = PFUser.query()
query?.findObjectsInBackgroundWithBlock({ (objects:[PFObject]?, error:NSError?) -> Void in

for object in objects!{
let recievedUser = (object as PFObject)["username"] as! String

if recievedUser == username{
userToAdd.removeAll()
userToAdd.append(username!)

self.addFriendLabel.setTitle("Share myEvents with \(userToAdd[0])", forState: .Normal)
}
}
})

}
}

@IBAction func searchButtonPressed(sender: AnyObject) {

checkForUser()
print(userToAdd)
if addFriendLabel.hidden == true{

let alertController = UIAlertController(title: "Username not found!", message: "A user with this username does not exist, please check the spelling or your internet connection", preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(action)
self.presentViewController(alertController, animated: true, completion: nil)

}
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

当我再次单击该按钮时,print(userToAdd) 仅显示[“测试”],第一次单击它时,它显示[]。我知道我也可以将它保存为字符串而不是数组,我只是在玩,因为使用字符串我遇到了完全相同的问题。

我希望有人理解我的意思 :D 并知道我的问题的解决方案。

最佳答案

正如函数 findObjectsInBackgroundWithBlock 的名称所示,查询操作在后台完成,因此在调用 checkForUser() 后立即尝试访问 userToAdd code> 将为您提供 nil,因为它尚未设置,因为查询尚未完成。第二次按下按钮时,您将访问现已完成的第一个查询的结果。

您可以使用完成闭包(在 Objective-C 中称为 block ,因此函数名称中包含 WithBlock 部分)来处理结果并在需要时显示错误。

您还可以使用 whereKey 只检索您要查找的用户,从而提高查询效率;

import UIKit
import Parse
import Bolts

var userToAdd=""

class AddFriendViewController: UIViewController {

@IBOutlet var addFriendLabel: UIButton!
@IBOutlet var searchUserTF: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
addFriendLabel.setTitle("", forState: .Normal)
// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func searchButtonPressed(sender: AnyObject) {

if searchUserTF.text != "" {
let username=searchUserTF.text
var query = PFUser.query()
query.whereKey("username", equalTo:username)
query?.findObjectsInBackgroundWithBlock({ (objects:[PFObject]?, error:NSError?) -> Void in

if (error != nil) {
print(error)
} else {
if objects!.count >0 {
self.addFriendLabel.setTitle("Share myEvents with \(username)", forState: .Normal)
self.userToAdd=username
} else {
self.userToAdd=""
let alertController = UIAlertController(title: "Username not found!", message: "A user with this username does not exist, please check the spelling or your internet connection", preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(action)
self.presentViewController(alertController, animated: true, completion: nil)
}
})
}
}

关于ios - PFUser 查询在第一次运行时不会更改变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34113205/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com