gpt4 book ai didi

arrays - 类型 'Any' 在 Swift 中没有下标成员

转载 作者:行者123 更新时间:2023-11-28 11:58:00 27 4
gpt4 key购买 nike

我为标题道歉。我知道这是 Swift 中的一个常见问题。我阅读了其他线程中的答案,我相信我离解决问题又近了一点。

我有一个字典数组 var userObjects: [[String:AnyObject]] = []

我用它来填充 Collection View 。在我的 cellForItemAt 函数中,我有

guard let user = userObjects[indexPath.row]["data"] as? [String:AnyObject] else { return cell }
guard let firstName = user["first_name"] else {return cell}

在我添加 as? [String:AnyObject] 到代码段的第一行。现在的问题是代码在 user 的第一个 guard 语句处中断,就好像该语句不正确一样。

我哪里做错了,我该如何正确转换对象?

userObjects 中元素的示例:

{
count = 1;
data = (
{
email = "tester2@test.com";
"first_name" = Tester;
gender = Male;
instagram = "n/a";
"last_name" = One;
preference = Female;
profileImageURL = "n/a";
}
);
page = 0;
pageCount = 1;
perPage = 20;

最佳答案

当你说

guard let user = userObjects[indexPath.row]["data"] as? [String:AnyObject] else { return cell }

...你说 user"data" 条目:

data = (
{
email = "tester2@test.com";
"first_name" = Tester;
gender = Male;
instagram = "n/a";
"last_name" = One;
preference = Female;
profileImageURL = "n/a";
}
);

那不是字典。它是字典的数组。因此,用 "first_name" 下标的尝试失败了。

关于arrays - 类型 'Any' 在 Swift 中没有下标成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50546050/

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