1

Is there a way of converting an array of type Dictionary.Values? into an one-dimension array of type String?

Code:

docRef.getDocument { (document, error) in
            if let document = document, document.exists {    

                let dataDescription = document.data()?.values  // Type 'Dictionary<String, Any>.Values?' 
                self.array.append(dataDescription)   // Tried dataDescription.values, but it doesn't work

                print("Document data: \(String(describing: dataDescription))")
            } else {
                print("Document does not exist")
            }
        }
0

1 Answer 1

1

You can unwrap data as well and then map the values from Any to String:

docRef.getDocument { document, error in
    if let document = document, document.exists,    
        let data = document.data()?.values {
            let values = data.values.compactMap{$0 as? String}
            print(values)
        } else {
                print("Document does not exist")
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.