- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从 firestore 检索数据,到目前为止一切都很成功。这是我的控制台中的结果。
[{
Name = Tuna;
Price = "$3.6"; }, {
Name = Snapper;
Price = "$25.60"; }]
我想将名称和价格存储到变量中,以便可以在 TableView 中显示它们。如下面的代码所示,当我将 nameOfItem 设置为 Data2 时,我收到一条错误,指出“无法使用“String”类型的索引为“[Any]”类型的值添加下标”。我想知道是否有人可以帮我解决这个问题!
override func viewWillAppear(_ animated: Bool) {
itemCollectionRef.getDocuments { (snapshot, error) in
if let err = error {
debugPrint("Error fetching docs: \(err)")
}else {
guard let snap = snapshot else {return}
for document in snap.documents {
let data2 = document.data()["Items"]! as? Array ?? []
print(data2)
let RandomVariable = data2[0]
print(RandomVariable)
let nameOfItem = data2["Name"] as? String ?? ""
let priceOfItem = data2["Price"] as? String ?? ""
//let priceOfItem = data2["Price"] as? String ?? ""
//print(nameOfItem, priceOfItem)
}
}
}
最佳答案
正如您自己所看到的,控制台输出中没有“Items”元素之类的东西,而是因为错误消息表明您有一个数组,因此将 for 循环内的代码更改为
let data = document.data() as? [Any] ?? []
for item in data {
if let dictionary = item as? [String: String] {
let nameOfItem = dictionary["Name"] ?? ""
let priceOfItem = dictionary["Price"] ?? ""
//...
}
}
关于swift - 无法使用 '[Any]' 类型的索引为 'String' 类型的值添加下标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57719489/
是否可以创建一个可以使用显式参数标签调用的下标? struct MyType { subscript (label: Bool) -> String? { return nil
我正在尝试制作一个包含带有上标和/或下标的文本的超链接。我找到了两种方法来做到这一点,它们都很糟糕。 解决方案#1:使用 Typography.Variants .这为某些字体提供了极好的上标……。
在这里! 我想在 geom_bracket 中包含一个带下标的标签在 ggplot2 .我尝试了不同的方式,但没有人成功(评论中的尝试): library(ggplot2) ggplot(data =
我正在尝试让 graphviz 启动并工作,我迫切需要节点标签中的下标。不幸的是,通过浏览无数关于类似问题的帖子,我似乎适合所有建议的解决方案,但仍然不起作用。这是我的代码: digraph G{
抱歉格式问题,我从来没有真正在这样的论坛上发帖,所以我必须学习一下操作方法。 我的问题是:我正在编写一个模板类,我想通过多种 [] 运算符访问我的容器。我读了一些关于这个主题的内容,所以我已经能够重载
我知道我们可以像在 matplotlib 中生成单个下标 $r_i$ 会给我一个下标为“i”的r。 但我想生成一个包含 3 或 4 个字母的下标,例如 r_ijk 应该给我一个带有“ijk”作为下标的
this[5] 有什么作用?我是否调用了某种未定义的行为?关于: std::vector foo{this, this + 5u}; 这个有用吗?我想知道指针算法对 this 的影响是什么。这是一个测
我从 visual studio 得到了一些奇怪的行为,关于以下代码片段,错误列表显示了 E0349 的几个实例:没有运算符“[]”匹配这些操作数。 Intellisense 似乎暗示类型不匹配,但正
我想为我的数组类提供 PHP 样式的 push_back 功能: arrayT arr; arr[] = 10; // == std::vector::push_back() 和 arrayT::op
下标 (subscripts)可以定义在类(class)、结构体(structure)和枚举(enumeration)中,是访问集合(collection),列表(list)或序列(sequence)
我正在使用traindata训练svm。 (R中的e1071软件包)。以下是有关我的数据的信息。 > str(train) 'data.frame': 891 obs. of 10 variab
#include int main(){ int arr[7] = {0,1,2,3,4,3,2}; arr[0]++[arr]++[arr]++[arr]++[arr]++[arr]
如果我想以特定用户的身份调用主脚本文件中的另一个 shell 脚本,我该怎么做呢?子脚本似乎失去了它正在运行的用户的上下文,我还没有找到任何有用的子脚本技术。 例如:war-install.sh if
这个问题在这里已经有了答案: Why isn't there an operator[] for a std::list? (4 个答案) 关闭 5 年前。 我有这些类型定义: typedef pa
我在 NSUserdefaults 中获取字典的字符串时遇到问题,这是我的代码。我不知道似乎是什么问题: static func getItemInUserDefaultsDictionary(key
我正在尝试执行以下代码并收到错误 Could not find member 'subscript on Xcode6 var b:[[Float]] = [[1,2]] var i2 = 0 //
我尝试运行的代码: std::string genBlankName(std::vector &posts) { std::string baseName = "New Post ";
1 1 A_{3} 2 2 C_{2} 3 3 ^{5}C_{1} 我有一个这样的输入文件要绘制。第三列是该点上的标签( latex 格式)。我如何在绘图上显示这些标签,就像它们在 latex 编译后
我在这里搜索了一段时间,之前的问题/答案部分回答了我的问题。我正在学习 R,来自 Matlab。正如标题所说,我有一个关于情节注释的问题。在 Matlab 中,绘制包含各种数据格式的注释非常简单,我正
我想将一些化学数据放入表格的列中。但在现有表格中,下标显示为普通字符。其中一些显示为问号。我应该怎么做才能解决它? 当我输入这段代码时 SELECT N'H' + NCHAR(0x2082) + N
我是一名优秀的程序员,十分优秀!