i want to save two textfield texts in one let. I need this so i can upload the data at the same time otherwise its gonna make 2 separate data. The newStoryYoutube
and newStorynaam
need to have one let. This is my code:
@IBAction func postDidTouch(_ sender: UIBarButtonItem)
{
if Youtubelink.text != "" {
// Create and save a new story
let newStoryYoutube = Story(text: Youtubelink.text!)
newStoryYoutube.save()
}
if Naamgeluidje.text != "" {
// Create and save a new story
let newStorynaam = Story(textnaam: Naamgeluidje.text!)
newStorynaam.save()
self.navigationController!.popViewController(animated: true)
}
}
Story class in a different viewcontroller:
class Story
{
var text: String = ""
var textnaam: String = ""
var numberOfLikes = 0
var numberOfAngry = 0
let ref: DatabaseReference!
init(text: String) {
self.text = text
ref = Database.database().reference().child("stories").childByAutoId()
}
init(textnaam: String) {
self.textnaam = textnaam
ref = Database.database().reference().child("stories").childByAutoId()
}
init(snapshot: DataSnapshot)
{
ref = snapshot.ref
if let value = snapshot.value as? [String : Any] {
text = value["text"] as! String
textnaam = value["textnaam"] as! String
numberOfLikes = value["numberOfLikes"] as! Int
numberOfAngry = value["numberOfAngry"] as! Int
}
}
func save() {
ref.setValue(toDictionary())
}
func toDictionary() -> [String : Any]
{
return [
"text" : text,
"textnaam" : textnaam,
"numberOfLikes" : numberOfLikes,
"numberOfAngry" : numberOfAngry
]
}
}