0

I let user to enter text into UITextView then I send it to firebase using this code

         func sendDataToFirebase(){
            
              let productID = self.ref.childByAutoId().key
              let userID = Auth.auth().currentUser?.uid
           
              let product = [ "productID" : productID,
                              "uid" : userID,
                              "details": self.txtProductDetails.text! as String
        
                ] as [String : Any]
       
            self.ref.child("products").child(productID!).setValue(product)

           }

If the text that user enter has multiline, it will go to firebase and has \n\n enter image description here

So when I fetch the text I am getting the same result of \n\n. How can I remove \n\n where the textView have to look organized.

enter image description here

  • Does this answer your question? [Removing line breaks from string](https://stackoverflow.com/questions/43073073/removing-line-breaks-from-string) – Joakim Danielson Aug 21 '20 at 19:47

1 Answers1

1

Try This

  func sendDataToFirebase(){
        
          let productID = self.ref.childByAutoId().key
          let userID = Auth.auth().currentUser?.uid
          guard let text = self.txtProductDetails.text else{return}
          let newText = text.replacingOccurrences(of: "\n\n", with: "")
       
          let product = [ "productID" : productID,
                          "uid" : userID,
                          "details": newText
    
            ] as [String : Any]
   
        self.ref.child("products").child(productID!).setValue(product)

       }
Anmol Rattan
  • 137
  • 1
  • 8
  • `guard let text = self.txtProductDetails.text else { return }` this is pointless. `UITextView` text property will NEVER return `nil`. – Leo Dabus Aug 22 '20 at 02:12
  • 2
    Yes, but it's good practice to safely unwrap the optional when you see one. – Anmol Rattan Aug 22 '20 at 12:26
  • 1
    Good answer. One issue. If */n/n* is replaced with "" then all of the spaces where /n/n should/could be are lost. So reading back it would look like *months to make.The Size* (no space between the "." and "The"). Probably better to add a space " ". Just a thought. – Jay Aug 23 '20 at 14:28
  • 1
    Yes, you are right. But the text image has space before \n\n (like large and medium \n\nThe weight). So, I thought it would structure better without space. – Anmol Rattan Aug 23 '20 at 22:57