xcode Swift & Parse - PFUser currentUser 永远不等于 nil
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30216819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift & Parse - PFUser currentUser never equals nil
提问by Midaero
I am using Xcode, Swift, and Parse. When I try and logout a PFUser, i never get a return of nil.
我正在使用 Xcode、Swift 和 Parse。当我尝试注销 PFUser 时,我永远不会得到零的回报。
In this part of the app, the viewController is simply showing a few buttons one logs in. One sends the user to signup. One sends the user to change details, and one is a simple logout.
在应用程序的这一部分中,viewController 只是显示几个登录按钮。一个发送用户进行注册。一种是发送用户更改详细信息,一种是简单的注销。
The code for the two that matter on logout is;
注销时重要的两个代码是;
@IBAction func logout(sender: AnyObject) {
PFUser.logOut()
var currentUser = PFUser.currentUser()
self.displayAlert("You are now logged out", error: "")
println(currentUser!)
}
@IBAction func changeDetails(sender: AnyObject) {
var currentUser = PFUser.currentUser()
println(currentUser!)
if currentUser != nil {
let nextView30 = self.storyboard?.instantiateViewControllerWithIdentifier("changeDetails") as! changeUserDetailsViewController
self.navigationController?.pushViewController(nextView30, animated: true)
} else {
self.displayAlert("Please log in", error: "")
}
}
Once the code runs and I logout, wherever the currentUser gets read I get the following type of response, not nil. The next ViewController is actioned, and this shouldn't happen without a usr logged in.
一旦代码运行并注销,无论 currentUser 在哪里被读取,我都会得到以下类型的响应,而不是 nil。下一个 ViewController 被操作,如果没有 usr 登录,这不应该发生。
PFUser: 0x37018fbc0, objectId: new, localId: local_3b5eb7453f9af5ed { }
PFUser: 0x37018fbc0, objectId: new, localId: local_3b5eb7453f9af5ed { }
Am I doing something wrong or is this standard? If it is correct, how do I return no user logged in?
我做错了什么还是这个标准?如果正确,如何返回没有用户登录?
Thanks
谢谢
回答by sfbayman
if PFUser.currentUser()!.username != nil
{
self.performSegueWithIdentifier("loginSegue", sender: self)
}
The above code worked for the login issue. But i still have the logout issue. After I call PFUser.logout(), PFUser.currentUser() is not becoming nil. Any help?
上面的代码适用于登录问题。但我仍然有注销问题。在我调用 PFUser.logout() 之后,PFUser.currentUser() 不会变为 nil。有什么帮助吗?
Thanks
谢谢
回答by Kitson
I've been struggling with logging out for a little while and I believe I have finally cracked it!
我一直在努力注销一段时间,我相信我终于破解了它!
No matter what I did, when I used "PFUser.logOut()" would never set "PFUser.currentUser()" to nil, but it would set "PFUser.currentUser()!.username" to nil...
不管我做了什么,当我使用“PFUser.logOut()”时,永远不会将“PFUser.currentUser()”设置为nil,但它会将“PFUser.currentUser()!.username”设置为nil...
Because of this I used
因为这个我用
var currentUser = PFUser.currentUser()!.username
as a global variable to track is a user is logged in.
作为要跟踪的全局变量是用户登录。
On my login/first page I added
在我的登录/第一页上,我添加了
override func viewDidAppear(animated: Bool) {
if currentUser != nil {
self.performSegueWithIdentifier("login", sender: self)
}
}
and finally on my logout button i used
最后在我使用的注销按钮上
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "logout" {
PFUser.logOut() //Log user out
currentUser = PFUser.currentUser()!.username //Reset currentUser variable to nil
}
}
I hope that helps!
我希望这有帮助!
回答by dev27
Try commenting out the following line of code in your AppDelegate.swift file -
尝试在 AppDelegate.swift 文件中注释掉以下代码行 -
PFUser.enableAutomaticUser()
enableAutomaticUser() will log in an anonymous user once you call PFUser.logOut(), and the username for an anonymous user is nil.
enableAutomaticUser() 会在调用PFUser.logOut() 后登录匿名用户,匿名用户的用户名为零。
回答by Maxymus Boopathy
override func viewDidLoad(animated: Bool) {
var currentUser = PFUser.currentUser()?.username
if(currentUser != nil){
var loginAlert: UIAlertController = UIAlertController(title: "Signup/ Login", message:"Please Signup or Login" , preferredStyle: UIAlertControllerStyle.Alert)
loginAlert.addTextFieldWithConfigurationHandler({
textfield in
textfield.placeholder = "Your Username"
})
loginAlert.addTextFieldWithConfigurationHandler({
textfield in
textfield.placeholder = "Your Password"
textfield.secureTextEntry = true
})
loginAlert.addAction(UIAlertAction(title: "Login", style: UIAlertActionStyle.Default, handler: {alertAction in
let textFields: NSArray = loginAlert.textFields! as NSArray
let usernameTextFields: UITextField = textFields.objectAtIndex(0) as! UITextField
let passwordTextFields: UITextField = textFields.objectAtIndex(1) as! UITextField
PFUser.logInWithUsernameInBackground(usernameTextFields.text as String!, password: passwordTextFields.text as String!){
(loggedInuser: PFUser?, signupError: NSError?) -> Void in
if((loggedInuser) != nil){
println("User logged in successfully")
}else{
println("User login failed")
}
}
}))
loginAlert.addAction(UIAlertAction(title: "SignUp", style: UIAlertActionStyle.Default, handler: {alertAction in
let textFields: NSArray = loginAlert.textFields! as NSArray
let usernameTextFields: UITextField = textFields.objectAtIndex(0) as! UITextField
let passwordTextFields: UITextField = textFields.objectAtIndex(1) as! UITextField
var sweeter : PFUser = PFUser()
sweeter.username = usernameTextFields.text
sweeter.password = passwordTextFields.text
sweeter.signUpInBackgroundWithBlock {(sucess,error) -> Void in
if !(error != nil){
println("sign up success")
}else
{
println("constant error string\(error)")
}
}
}))
self.presentViewController(loginAlert, animated: true, completion: nil)
}
}