gpt4 book ai didi

ios - 将对象数组转换为字符串数组

转载 作者:行者123 更新时间:2023-11-28 14:10:07 25 4
gpt4 key购买 nike

如何将此对象数组转换为字符串数组?我正在读取 Firebase 集合中的记录。

    firebaseDB.collection("message").document(key).collection("messages").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
}
else {
self.driverArr.removeAll()
for document in querySnapshot!.documents {
print("\(document.documentID) => \(document.data())")
let msgdata = document.data() as! [String:Any]
var msgObj = Message()
if let name = msgdata["name"] as? String {
msgObj.name = name
}
self.driverArr.append(msgObj)
}
if self.driverArr.count < 1 {
print("No Drivers")
}
else{
print("*** Driver Names array ***")
print(self.driverArr)
}
}
}

这个数组的当前输出是:

[MainApp.Message(name: Optional("Oliver"), userId: Optional(""), msg: Optional(""), creatAt: Optional(0), latitude: Optional(""), longitude: Optional("")), MainApp.Message(name: Optional("Bluesona"), userId: Optional(""), msg: Optional(""), creatAt: Optional(0), latitude: Optional(""), longitude: Optional("")), MainApp.Message(name: Optional("Oliver"), userId: Optional(""), msg: Optional(""), creatAt: Optional(0), latitude: Optional(""), longitude: Optional(""))]

我希望简单地输出一个数组,其中包含名称为 ["Oliver", "Bluesona"...]

的字符串数组

最佳答案

您可以使用 compactMap 获取数组中的所有有效名称。

print( driverArr.compactMap({$0.name}) )

关于ios - 将对象数组转换为字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52663129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com