Ios 使用Swift读取Firebase中快照内部的阵列
尝试读取此表单的数组时需要帮助: 据我所知,我得到了这个Ios 使用Swift读取Firebase中快照内部的阵列,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,尝试读取此表单的数组时需要帮助: 据我所知,我得到了这个 let defaults = UserDefaults.standard let userUuid = defaults.string(forKey: defaultsKeys.keyOne) let ref = FIRDatabase.database().reference().child("images").child("\(userUuid!)") let filterQuery = ref.queryOrdered(byChi
let defaults = UserDefaults.standard
let userUuid = defaults.string(forKey: defaultsKeys.keyOne)
let ref = FIRDatabase.database().reference().child("images").child("\(userUuid!)")
let filterQuery = ref.queryOrdered(byChild: "uuid").queryEqual(toValue: "\(uuid)") // where uuid is a value from another view
filterQuery.observe(.value, with: { (snapshot) in
for images in snapshot.children {
print(images)
}
})
但我什么也没收到。我想阅读图像的链接以在视图控制器中显示它们
uuid
变量不是可选值(如果是,请将其展开),否则您将查询以与“可选(myUuidValue)”
而不是“myUuidValue”
快照
包含的不仅仅是图像,它还包含该uuid下的所有其他子项
filterQuery.observe(.value, with: { (snapshot) in })
所以像这样提取图像:
filterQuery.observe(.value, with: { (snapshot) in
let retrievedDict = snapshot.value as! NSDictionary
let innerDict = retrievedDict["KeyHere"] as! NSDictionary // the key is the second inner child from images (3172FDE4-...)
let imagesOuterArray = userDict["images"] as! NSArray
for i in 0 ..< imagesOuterArray.count {
let innerArray = imagesOuterArray[i] as! NSArray
for image in innerArray {
print(image as! String)
}
}
})
filterQuery.observe(.value,其中:{(快照)在
让retrievedDict=snapshot.value为!NSDictionary
设innerDict=retrievedDict[“KeyHere”]为!NSDictionary//该键是来自图像的第二个内部子级(3172FDE4-…)
让imagesourterarray=userDict[“images”]作为!NSArray
对于0中的i..
澄清:将uuid的所有子项强制转换为NSDictionary
,然后使用这两个for循环提取嵌套数组uuid
变量不是可选值(如果是,请将其展开),否则您将查询以与“可选(myUuidValue)”
而不是“myUuidValue”
快照
包含的不仅仅是图像,它还包含该uuid下的所有其他子项
filterQuery.observe(.value, with: { (snapshot) in })
所以像这样提取图像:
filterQuery.observe(.value, with: { (snapshot) in
let retrievedDict = snapshot.value as! NSDictionary
let innerDict = retrievedDict["KeyHere"] as! NSDictionary // the key is the second inner child from images (3172FDE4-...)
let imagesOuterArray = userDict["images"] as! NSArray
for i in 0 ..< imagesOuterArray.count {
let innerArray = imagesOuterArray[i] as! NSArray
for image in innerArray {
print(image as! String)
}
}
})
filterQuery.observe(.value,其中:{(快照)在
让retrievedDict=snapshot.value为!NSDictionary
设innerDict=retrievedDict[“KeyHere”]为!NSDictionary//该键是来自图像的第二个内部子级(3172FDE4-…)
让imagesourterarray=userDict[“images”]作为!NSArray
对于0中的i..
澄清:将uuid的所有子项强制转换为NSDictionary
,然后使用这两个for循环提取嵌套数组感谢Jay指出错误!另外,正如杰伊所建议的,考虑重构数据库并将这些数组替换为可能包含URL、路径(如果需要删除的目的)和每个图像的时间戳的字典。 < P>求出答案后,得到该代码工作
let ref = FIRDatabase.database().reference().child("images").child("\(userUuid!)")
let filterQuery = ref.queryOrdered(byChild: "identifier").queryEqual(toValue: "\(identifier)")
filterQuery.observe(.value, with: { (snapshot) in
for child in snapshot.children {
if (child as AnyObject).hasChild("images") {
let images = (images as AnyObject).childSnapshot(forPath: "images").value! as! NSArray
for i in images {
for j in i as! [AnyObject] {
let url = NSURL(string: j as! String)
//Then downloaded the images to show on view
URLSession.shared.dataTask(with: url! as URL, completionHandler: { (data, response, error) in
if error != nil {
print(error)
return
}
//Code to show images..
}).resume()
}
}
}
}
})
我能收到关于这个的反馈吗?在苦苦寻找答案后,这段代码开始工作了
let ref = FIRDatabase.database().reference().child("images").child("\(userUuid!)")
let filterQuery = ref.queryOrdered(byChild: "identifier").queryEqual(toValue: "\(identifier)")
filterQuery.observe(.value, with: { (snapshot) in
for child in snapshot.children {
if (child as AnyObject).hasChild("images") {
let images = (images as AnyObject).childSnapshot(forPath: "images").value! as! NSArray
for i in images {
for j in i as! [AnyObject] {
let url = NSURL(string: j as! String)
//Then downloaded the images to show on view
URLSession.shared.dataTask(with: url! as URL, completionHandler: { (data, response, error) in
if error != nil {
print(error)
return
}
//Code to show images..
}).resume()
}
}
}
}
})
我能收到有关这方面的反馈吗?请将您的Firebase结构作为文本而不是图像发布。这样,我们就不必重新键入,而且可以搜索。不要使用数组!它们非常随机应变,也有更灵活的备选方案。另外,snapshot.children中返回的图像不是图像-它们将是所有子节点(图像、名称、样式等)的每个父节点。您是否可以共享任何其他灵活的解决方案来按记录读取或保存图像?当然可以!当您以文本形式发布Firebase结构时,我们可以进一步检查问题和解决方案。您想要json格式的吗?因为我有firebase web上的结构,所以是否有将其复制为文本的选项?在firebase控制台中,选择您的数据库,然后在数据库区域的右上角(三个垂直点)单击并选择导出JSON。请将您的firebase结构作为文本而不是图像发布。这样,我们就不必重新键入,而且可以搜索。不要使用数组!它们非常随机应变,也有更灵活的备选方案。另外,snapshot.children中返回的图像不是图像-它们将是所有子节点(图像、名称、样式等)的每个父节点。您是否可以共享任何其他灵活的解决方案来按记录读取或保存图像?当然可以!当您以文本形式发布Firebase结构时,我们可以进一步检查问题和解决方案。您想要json格式的吗?因为我有来自firebase web的这种结构,所以有没有将其复制为文本的选项?在firebase控制台中,选择您的数据库,然后在数据库区域的右上角(三个垂直点)单击并选择export JSON。我确信这段代码不会起作用(实际上我已经尝试过了)。有几个问题。一个是retrievedDict变量包含一个键值对,键值为3172。。。和另一本字典的值。因此,下一行imagesOuterArray失败,因为此时没有“images”键。但最重要的是避免在Firebase中使用数组,因为它们不能通过插入、删除等进行修改,并且元素不能单独访问。(还要注意,使用NSArray和NSDictionary时,代码不是很快)。不过,只要稍加调整,就可以了!实际上我同意Firebase中数组的用法,但是OP问题中的结构有数组,所以我严格遵守它。但在NSArray/NSDictionary这一部分,我不同意你的看法。Firebase清楚地说明了设置和检索对象的类型。这对NSArray和Array没有多大区别,但Dictionary需要一个类型初始值设定项(
Dictionary
),这将是一个难题。谢谢你指出这个错误,不过我很快就会修复它!您只需将其定义为[String:AnyObject],是的,定义为它们的文档