我有波纹管功能,但它似乎没有通过 if let snapshotValue 行,我相信它是因为 snapshotUrl.value 不是字典。如果是这样我如何将其转换为一个?
func fetchAllPostImages() {
print("fetchAllPostImages function")
self.ref.child("osts").child(self.userID).child(self.postNum).child("Images").observe(.childAdded, with: { snapshot in
var snapshotUrl = snapshot.childSnapshot(forPath: "ImageUrl")
var snapshotTimeStamp = snapshot.childSnapshot(forPath: "timeStamp")
print("fhdsjaklhfkjdsfksahdjksfjlashfkljdhs")
print(snapshot.childSnapshot(forPath: "ImageUrl"))
print(snapshotUrl.value, "value")
if let snapShotValue = snapshotUrl.value as? [String: String] {
print("jkdfbhksafgsaghsjbbvcbcbcbccbbbbbbbbbbbbbbb")
for (_, value) in snapShotValue {
print(value)
if let imageURL = URL(string: value) {
print(imageURL, "image url here")
do {
let imageAsData = try Data(contentsOf: imageURL)
let image = UIImage(data: imageAsData)
// let ImageObject = Media()
// ImageObject.image = image
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")
}
}
}
}
})
}
函数的输出:
fetchAllPostImages function
fhdsjaklhfkjdsfksahdjksfjlashfkljdhs
Snap (ImageUrl)
https://firebasestge.googleapis.com/v0/b/copyyubidatabase.appspot.com/o/rVNvbWrC9aarXOTSHNXGoaIeR5B3%2Fpost:563592299%2Fimage1?alt=media&token=a0c4aeda-769a-48ac-85c8-ab595ccfdf08
Optional(https://fireestorage.googleapis.com/v0/b/copyyubidatabase.appspot.com/o/rVNvbWrC9aarXOTSHNXGoaIeR5B3%2Fpost:563592299%2Fimage1?alt=media&token=a0c4aeda-769a-48ac-85c8-ab595ccfdf08)
value
看起来 snapshotUrl.value 是一个可选包装的 URL。如果你想把它转换成字典,你只需要一个键和一个值。
我们无权访问 snapshot
的数据结构来告诉您如何准确使用它。
这是一些演示我的意思的 Playground 代码。这是按原样工作的:
import UIKit
import PlaygroundSupport
struct SnapShot {
func childSnapshot(forPath: String) -> URLWrapper? {
return URLWrapper(url: URL(string: "https://image.shutterstock.com/image-vector/sample-red-square-grunge-stamp-260nw-338250266.jpg")!)
}
}
struct URLWrapper {
var url:URL
var value:String? {
get {
return url.absoluteString
}
}
}
func fetchAllPostImages() {
let session = URLSession.shared
print("fetchAllPostImages function")
let snapshot = SnapShot()
if let snapshotUrl = snapshot.childSnapshot(forPath: "ImageUrl"), let snapShotValue = snapshotUrl.value, let imageURL = URL(string: snapShotValue) {
let task = session.dataTask(with: imageURL, completionHandler: { (data, response, error) in
if let data = data {
let image = UIImage(data: data)
print(image ?? "none")
}
})
task.resume()
}
}
PlaygroundPage.current.needsIndefiniteExecution = true
fetchAllPostImages()
关于ios - 函数不会运行所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53270568/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |