我需要你的帮助。我正在使用firebase和swift开发一个IOS应用程序。我有一个JSON文件中的用户列表,使用我试图验证用户的JSON文件。 目前,我的代码可以检索电子邮件的价值。如果我将输入作为电子邮件发送,我的目标是获取ID。例如:如果我发送电子邮件为sandeep@gmail.com然后我需要它ID为231.为此我写了一个函数findParent(),但我得到这个错误“无法将类型'UITextfield'的值转换为预期的参数输入'string'“。请帮我。 以下是补充:
Json文件:
[
{
"ID": 231,
"iN_Hub Membership": 12345,
"title": "Mr.",
"firstname": "kumar",
"lastname": "sandeep",
"email": "sandeep@gmail.com",
},
Swift代码:
import UIKit
import Firebase
import FirebaseAuth
//import FirebaseDatabase
class ViewController: UIViewController {
@IBOutlet weak var emailofUser: UITextField!
@IBOutlet weak var passwordofUser: UITextField!
var ref:FIRDatabaseReference! //created a variable ref of type firebase database reference
var databaseHandle:FIRDatabaseHandle? //to handle to database listener like to stop or start it
override func viewDidLoad() {
super.viewDidLoad()
ref = FIRDatabase.database().reference()
let newRef = ref.child("4")
newRef.queryOrderedByKey().observeEventType(.Value, withBlock: { snapshot in
let userId = snapshot.value!["ID"]
let memberhipNum = snapshot.value!["iN_Hub Membership"]
let emailID = snapshot.value!["email"]
print(userId)
print(memberhipNum)
print(emailID)
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func LoginPage(sender: UIButton) {
func findParent(emailid: String) {
//set firebase reference
ref = FIRDatabase.database().reference()
ref.child(emailid ).queryOrderedByKey().observeEventType(.Value, withBlock: { snapshot1 in
let myuserID = snapshot1.value!["ID"]
print(myuserID)
})
}
findParent(emailofUser)
if let email = emailofUser.text, pass = passwordofUser.text {
FIRAuth.auth()?.signInWithEmail(email, password: pass, completion: { (user, error) -> Void in
if error == nil{
print("Now its working. Well done Sandeep!!")
self.performSegueWithIdentifier("UserDetails", sender: self)
}
else{
print("nonononono")
let alertController = UIAlertController(title: "oops!", message: "user name and password combination is wrong!", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
alertController.addAction(defaultAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
})
}
}
}
希望有人能帮助我。提前谢谢。
答案 0 :(得分:0)
可能是问题的一个错误:
findParent(emailofUser)
应该是:
findParent(emailofUser.text)