How to convert Dictionary<String, Any>.Values? into String-array SWIFT

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")
            }
        }

Upvotes: 1

Views: 929

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236315

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")
        }
    }
}

Upvotes: 1

Related Questions