我目前有一个功能,该功能可以基于一些初步信息从firebase提取图像,并且在相同的树级别上,图像是时间戳值(针对每个图像)。我一直无法获得价值,只能获得Optional(123131.432432)
,这当然不是我所需要的。
以下是函数:
func fetchAllPostImages() {
print("fetchAllPostImages function")
self.ref.child("Posts").child(self.userID).child(self.postNum).child("Images").observe(.childAdded, with: { snapshot in
var snapshotUrl = snapshot.childSnapshot(forPath: "ImageUrl")
var timeStampData = snapshot.childSnapshot(forPath: "timeStamp").value
print("fhdsjaklhfkjdsfksahdjksfjlashfkljdhs")
print(snapshot.childSnapshot(forPath: "ImageUrl"))
print(snapshot.childSnapshot(forPath: "timeStamp").value, "timestamp123")
print(snapshotUrl.value as? [String: String], "value")
if let urlValue = snapshotUrl.value {
print("jkdhsjbbvcbcbcbccbbbbbbbbbbbbbbb")
let snapShotValue = [ "ImageUrl" : urlValue]
let timeStampVal = ["timeStamp" : timeStampData]
print(timeStampVal, "dsfaf")
for (_, value) in snapShotValue {
if let imageURL = URL(string: value as! String) {
print(imageURL, "image url here")
do {
let imageAsData = try Data(contentsOf: imageURL)
let image = UIImage(data: imageAsData)
var timeStamp = 0.0
self.arrayOfImgObj.append(Media(image: image!, timeStamp: timeStamp))
self.tableView.reloadData()
print(self.arrayOfImgObj.count, "array.count")
} catch {
print("imageURL was not able to be converted into data")
}
}
}
}
})
}
所以我能够执行以下操作,但这似乎可行,但是在tableView中,我看到了与以前相同的Optional(5421432.43214)。即使我打印时,它也需要两倍的价值。
for (value, timeValue) in zip(snapShotValue.values, timeStampVal) {
if let imageURL = URL(string: value as! String) {
print(imageURL, "image url here")
do {
let imageAsData = try Data(contentsOf: imageURL)
let image = UIImage(data: imageAsData)
var timeStampValue = timeValue as! Double
print(timeStampValue, "fgsdfg")
let timeStamp: Double = timeStampValue
self.arrayOfImgObj.append(Media(image: image!, timeStamp: timeStamp))
self.tableView.reloadData()
print(self.arrayOfImgObj.count, "array.count")
} catch {
print("imageURL was not able to be converted into data")
}
}
}
如何将包装在可选内容中的timeStamp值转换为双精度值?
答案 0 :(得分:1)
基本上,最简单的方法是将Int
添加到调用arrayOfImg[indexPath.row].timeStamp
的位置。所以它应该像这样:
let timeStamp = "\(Int(arrayOfImg[indexPath.row].timeStamp))"
答案 1 :(得分:-1)
尝试更换
let timeStamp: Double = timeStampValue
使用
guard let timeStamp = timeStampValue as Double else { return }
这将安全地打开您的可选内容,并将值转换为Double。