gpt4 book ai didi

swift - PFUser() 和处理密码成员

转载 作者:行者123 更新时间:2023-11-30 14:05:34 26 4
gpt4 key购买 nike

对于一个很长的问题,我深表歉意。大部分代码都是多余的。请看一下!!!

起初,我认为指定的信息是在 else 部分发送的,正如 Parse 文档所示,https://parse.com/docs/ios/guide#users :

func myMethod() {
var user = PFUser()
user.username = "myUsername"
user.password = "myPassword"
user.email = "email@example.com"
// other fields can be set just like with PFObject
user["phone"] = "415-392-0202"

user.signUpInBackgroundWithBlock {
(succeeded: Bool, error: NSError?) -> Void in
if let error = error {
let errorString = error.userInfo?["error"] as? NSString
// Show the errorString somewhere and let the user try again.
} else {
// Hooray! Let them use the app now.
}
}
}

请注意,“万岁!现在就让他们使用该应用程序吧。”所以我想只要控制流没有到达else{},指定的信息就不会发送到云端。但是我错了。经过多次尝试和错误,这是我得出的结论。如果 error 等于 nilsignUpInBackgroundWithBlock 会发送必要的信息。如果我错了,请纠正我。这就是原因。只要username成员是唯一的并且采用“something@something.com”的形式,signUpInBackgroundWithBlock就会将指定的信息发送到云端,甚至如果,则其他字段为空。

以下代码相当多余,对此我深表歉意。只需忽略所有 alertController 代码即可。

import UIKit
import Parse

class SignUpViewController: UIViewController {

@IBOutlet weak var firstName: UITextField!
@IBOutlet weak var lastName: UITextField!
@IBOutlet weak var emailAddress: UITextField!
@IBOutlet weak var password: UITextField!

override func viewDidLoad() {
super.viewDidLoad()
firstName.becomeFirstResponder()
}

@IBAction func signUp(sender: AnyObject) {
let user = PFUser()
user["firstName"] = firstName.text
user["lastName"] = lastName.text
user.email = emailAddress.text
user.password = password.text
user.username = emailAddress.text

user.signUpInBackgroundWithBlock { (succeeded: Bool, error: NSError?) -> Void in
if self.firstName.text == "" {
let alertController = UIAlertController(title: "", message: "please enter your first name", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
self.presentViewController(alertController, animated: true, completion: nil)
} else if self.lastName.text == "" {
let alertController = UIAlertController(title: "", message: "please enter your last name", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
self.presentViewController(alertController, animated: true, completion: nil)
} else if self.emailAddress.text == "" {
let alertController = UIAlertController(title: "", message: "please enter your email", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
self.presentViewController(alertController, animated: true, completion: nil)
} else if self.password.text == "" {
let alertController = UIAlertController(title: "", message: "please enter a password", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
self.presentViewController(alertController, animated: true, completion: nil)
} else if let error = error {
var errorString = error.userInfo?["error"] as? NSString
errorString = String(errorString!)

if (errorString?.rangeOfString("missing username").toRange() != nil) {
errorString = "missing email address"
}

if (errorString?.rangeOfString("username \(self.emailAddress.text) already taken").toRange() != nil) {
errorString = "email \(self.emailAddress.text) already taken"
}

let alertController = UIAlertController(title: "", message: "\(errorString!)", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
self.presentViewController(alertController, animated: true, completion: nil)
} else {
println("SDFJPOIWEJRPOIJWEPOITPOEWIJRPOWIEJRPIWOJEPTIHWEPROIJWPEROIJ")
}
}
}
}

此外,有趣的是,如果 password 成员为空,Parse 似乎应该生成 错误,因为它是 PFUser() 的一部分> 当用户名为空时,会生成错误。但事实似乎并非如此。

所以底线是:如果其他字段为空,我不希望 signUpInBackgroundWithBlock 将指定信息发送到云。我不知道该怎么做。有人有解决这个问题的建议吗?

最佳答案

您只需将验证代码移至 User.signUpInBackgroundWithBlock 回调之外即可:

@IBAction func signUp(sender: AnyObject) {
let user = PFUser()
user["firstName"] = firstName.text
user["lastName"] = lastName.text
user.email = emailAddress.text
user.password = password.text
user.username = emailAddress.text

if firstName.text == "" {
let alertController = UIAlertController(title: "", message: "please enter your first name", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
presentViewController(alertController, animated: true, completion: nil)
} else if lastName.text == "" {
let alertController = UIAlertController(title: "", message: "please enter your last name", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
presentViewController(alertController, animated: true, completion: nil)
} else if emailAddress.text == "" {
let alertController = UIAlertController(title: "", message: "please enter your email", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
presentViewController(alertController, animated: true, completion: nil)
} else if password.text == "" {
let alertController = UIAlertController(title: "", message: "please enter a password", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
presentViewController(alertController, animated: true, completion: nil)
} else {
user.signUpInBackgroundWithBlock { (succeeded: Bool, error: NSError?) -> Void in
if let error = error {
var errorString = error.userInfo?["error"] as? NSString
errorString = String(errorString!)

if (errorString?.rangeOfString("missing username").toRange() != nil) {
errorString = "missing email address"
}

if (errorString?.rangeOfString("username \(self.emailAddress.text) already taken").toRange() != nil) {
errorString = "email \(self.emailAddress.text) already taken"
}

let alertController = UIAlertController(title: "", message: "\(errorString!)", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
self.presentViewController(alertController, animated: true, completion: nil)
} else {
println("SDFJPOIWEJRPOIJWEPOITPOEWIJRPOWIEJRPIWOJEPTIHWEPROIJWPEROIJ")
}
}
}
}

关于swift - PFUser() 和处理密码成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32457590/

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