gpt4 book ai didi

Swift 数组设置 : Showing Recent Messages Command failed due to signal: Segmentation fault: 11

转载 作者:行者123 更新时间:2023-11-30 12:02:32 25 4
gpt4 key购买 nike

我正在使用 firebase 检索数据列表,然后将其转换为 NSDictonary 数组。我想通过属性解析数据,例如 name

func getAllMyModels() {
if let e = email {
_ = ref.child("childName").queryOrdered(byChild: "email").queryEqual(toValue: e).observe(.value) { snapshot in
var dictionary = [NSDictionary]()
let children = snapshot.children
while let rest = children.nextObject() as? DataSnapshot, let value = rest.value {
dictionary.append(NSDictionary(dictionary: value as! [String: Any]))
}
let names = dictionary.flatMap {$0["name"]} // names print correct values
let set = Set(names)
print(set)
}
}
}

此代码无法编译,错误为:

Showing Recent Messages
Command failed due to signal: Segmentation fault: 11

如果我删除这一行:

let set = Set(Array(names))

一切正常。

我还通过用这个 block 替换它来进行测试

  let ar = ["name1","name2"].flatMap { return $0 }
Set(ar)

没有错误。

不知道为什么?谁能告诉一下,谢谢!

编辑:即使数组中的元素是String类型,但名称数组是[Any],所以解决方案是

  let names = dictionary.flatMap {$0["name"]} as! [String]

最佳答案

我认为发生此错误是因为您使用 flatMap 从字典生成的数组是 Any 数组而不是字符串数组,请尝试将其转换为字符串,如下所示:

...
let names = dictionary.flatMap {$0["name"] as? String}
let set = Set(Array(names))
...

希望这对你有帮助

关于Swift 数组设置 : Showing Recent Messages Command failed due to signal: Segmentation fault: 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47033193/

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