【问题标题】:How to convert Dictionary<String, Any>.Values? into String-array SWIFT如何转换 Dictionary<String, Any>.Values?进入字符串数组 SWIFT
【发布时间】:2019-10-26 03:10:37
【问题描述】:

有没有办法转换 Dictionary.Values 类型的数组?转换成 String 类型的一维数组?

代码:

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

【问题讨论】:

    标签: ios arrays swift type-conversion


    【解决方案1】:

    您也可以解包数据,然后将值从Any 映射到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")
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      • 1970-01-01
      相关资源
      最近更新 更多