compare textfield.text to firebase string swift
Please don't use email addresses as keys. Email addresses are dynamic and may change (as in if the users wants to change it) and if they do, you'll have a mess on your hands as every node that directly used that email would have be deleted and re-created.
Best practice is to disassociate key's from the data they contain.
Here's the structure to use
emails
-Yiaisjpa90is
email: "dude@test.com"
-Yijs9a9js09a
email: "thing@test.com"
then you simply query the email node for the email you are looking for, and handle accordingly if it exists.
And some code
emailsRef.queryOrdered(byChild: "email").queryEqual(toValue: "dude@test.com")
.observe(.value, with: { snapshot in
if snapshot.value is NSNull {
print("the snapshot was null, no email found")
} else {
print("email was found, YIPEE")
}
})
For completeness it would be a little more Swifty to use
if snapshot.exists() {
print("found it")
} else {
print("no email found")
}
How to check if textField is equal to a string from an array
You can do this:
if let atm = atms.filter({$0.name == nameTextField.text}).first {
//Show alert
let alert = UIAlertController(title: "This name has been added!", message: nil, preferredStyle: .Alert)
let action = UIAlertAction(title: "Ok", style: .Default, handler: nil)
alert.addAction(action)
presentViewController(alert, animated: true, completion: nil)
} else {
//do another stuff
}
It will check the first atm that matches
Checking if firebase snapshot is equal to text field input
The value you're looking to filter on is the key of the node. So you'll need to call queryOrderedByKey()
. Something like:
databaseRef.child("locations")
.queryOrderedByKey()
.queryStarting(atValue: addressTextField.text)
.observe(DataEventType.value, with:
{
...
Firebase restoring initial values in textfields after changing text value
It's probably because you are putting your textField populating code, in viewDidLayoutSubviews() method.
This method will be triggered every time the layout of a views changes in viewcontroller.
Move it to viewDidLoad() and it should be fine.
The reason that it's being reverted to the previous value is because. You are populating the textField.text from the "ingredient" object. The ingredient will have the same value retained that you passed from previous view controller. Until you mutate it at some point.
And by the way Firebase is very cool I like it too :)
(Swift) Save data from UITextField into Firebase Firestore
You may want to collect the string from the field in the action and then pass it to a function for saving.
class TrainViewController: UIViewController{
@IBOutlet weak var trainField: UITextField!
@IBOutlet weak var locationField: UITextField!
@IBOutlet weak var engineField: UITextField!
@IBAction func saveButton(_ sender: UIButton) {
let trainText = trainField.text!
self.saveText(theText: trainText)
}
override func viewDidLoad() {
super.viewDidLoad()
}
func saveText(theText: String) {
let db = Firestore.firestore()
let dict = ["train_text": theText]
db.collection("users").addDocument(data: dict)
}
}
Will result in the following being written to Firestore
your_firestore
users
random_doc_id
train_text: "the passed in string"
EDIT
Based on some new info, the OP would like to take the text from three text fields and write them all to a single document.
So change the button action thusly - all it does is calls our saveText function and does not pass any data.
@IBAction func saveButton(_ sender: UIButton) {
self.saveText()
}
then the saveText function
func saveText() {
let trainText = self.trainField.text!
let locationText = self.locationField.text!
let engineText = self.engineField.text!
let db = Firestore.firestore()
let dict = ["train_text": trainText,
"location_text": locationText,
"engine_text": engineText]
db.collection("users").addDocument(data: dict)
}
and the resulting Firestore looks like this
your_firestore
users
random_doc_id
train_text: "text from the train field"
location_text: "text from the location field"
engine_text: "text from the engine field"
Related Topics
How Do People Deal with Iterating a Swift Struct Value-Type Property
Swiftui Call Function on Variable Change
Swift Error: Missing Return in a Function Expected to Return 'String'
Why How to Use Codable with a Project Language Version of Swift 3.3
How to Install Xcode on an External Hard Drive Along with the iPhone Simulator.App
How to Improve Camera Quality in Arkit
Checking If a Value Is Changed Using Kvo in Swift 3
Binding an Element of an Array of an Observableobject:'Subscript(_:)' Is Deprecated
Two Tables on One View in Swift
Difference Between Associated and Raw Values in Swift Enumerations
iOS Facebook Sdk: Login Doesn't Return Email Despite Permissions Granted
Count Elements of Array Matching Condition in Swift
Convert a Custom Object to Data to Be Saved in Nsuserdefauts