0

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
        ]
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Dani Kemper
  • 343
  • 2
  • 10
  • 1
    What is `Story(textnaam:)` type? If it is string then use [string concatenation +](https://stackoverflow.com/a/24034334/1025063) – Imad Ali Sep 10 '17 at 07:42

1 Answers1

0

You are creating two instance of your Story class here. What you should do is make a constructor without any parameters and then initialise your class once and then set the properties like below.

let newStory = Story()

 @IBAction func postDidTouch(_ sender: UIBarButtonItem)
    {
        if Youtubelink.text != "" {
            // Create and save a new story
            newStory.text = Youtubelink.text!
            newStory.save()


        }


        if Naamgeluidje.text != "" {

            // Create and save a new story

            newStory.textnaam = Naamgeluidje.text!
            newStory.save()

            self.navigationController!.popViewController(animated: true)
        }

    }

Change your Story class to this so you can initialise it without passing any parameters

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() {
        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
        ]
    }
}
Siyavash
  • 970
  • 9
  • 23