【发布时间】:2018-10-02 13:47:03
【问题描述】:
源码如下,
数据库:
static func insertErrorLog(moduleName: String, message: String) {
let dateToday = Date()
let timestamp = Int(Date().timeIntervalSince1970)
let db = Firestore.firestore()
let docData: [String: Any] = [
KDB.Fields.moduleName : moduleName,
KDB.Fields.message : message,
KDB.Fields.createdAt : dateToday
]
型号:
var uploadedAt: String = ""
var createdAt: String = ""
init(document: DocumentSnapshot) {
self.key = document.documentID
let json = JSON(document.data())
self.storeId = json[KDB.Fields.storeId].stringValue
self.imgName = json[KDB.Fields.imgName].stringValue
self.title = json[KDB.Fields.title].stringValue
self.description = json[KDB.Fields.description].stringValue
self.sort = json[KDB.Fields.sort].intValue
self.uploadedAt = json[KDB.Fields.uploadedAt].stringValue
self.createdAt = json[KDB.Fields.createdAt].stringValue
}
init(key: String, storeId: String, imgName: String, title: String, description: String, sort: Int, uploadedAt: String, createdImageAt: String = "") {
self.key = key
self.storeId = storeId
self.imgName = imgName
self.title = title
self.description = description
self.sort = sort
self.uploadedAt = uploadedAt
self.createdAt = createdImageAt
}
控制器:
query.getDocuments { (querySnapshot, error) in
if let error = error {
SpeedLog.print("Error: \(error.localizedDescription)")
} else {
var loop = 1
for document in (querySnapshot?.documents)! {
SpeedLog.print("documentDATA:\(document.data())")
var photoObject = Photo(document: document)
SpeedLog.print("photoObject:\(photoObject)")
if (!photoObject.imgName.isEmpty) {
let storagePath = "\(photoObject.imgName)"
SpeedLog.print("Photo storagePath: \(storagePath)")
let storage = Storage.storage()
let storageRef = storage.reference()
let imageRef = storageRef.child(storagePath)
imageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
if let error = error {
SpeedLog.print("Photos download image error documentID:\(document.documentID) : \(error.localizedDescription)")
} else {
photoObject.imageData = UIImage(data: data!)
self.photos.append(photoObject)
if loop == 1 {
self.imageView.image = photoObject.imageData!
self.photoTitleLabel.text = "\(photoObject.title)"
self.photoCreatedAtLabel.text = "\(photoObject.createdAt)"
self.photoCreatedAtTitleLabel.isHidden = false
//}
}
}
self.collectionView.reloadData()
loop += 1
}
}
}
}
}
}
extension PhotoGalleryViewController: UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return photos.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellPhotoGallery", for: indexPath) as! PhotoGalleryCollectionViewCell
configureCell(cell, atIndexPath: indexPath)
return cell
}
func configureCell(_ cell: PhotoGalleryCollectionViewCell, atIndexPath indexPath: IndexPath) {
let photo = photos[indexPath.row]
if let image = photo.imageData {
cell.imageView.image = image
}
photoLabel.text = photo.title
photoCreatedAtLabel.text = photo.createdAt
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let photo = photos[indexPath.row]
if let image = photo.imageData {
imageView.image = image
}
photoLabel.text = photo.title
photoCreatedAtLabel.text = photo.createdAt
}
在 Firebase 中,我们有字段 createdAt: (Timestamp)
基本上,当我在 Xcode 中运行代码时,照片存储会从 firebase 数据中获取 createdAt,但是当涉及到 Photo Object"self.photoCreatedAtLabel.text = "(photoObject.createdAt)" 时,日期没有显示。代码中可能缺少什么?请参阅附件以供参考。
编辑:添加了照片对象的控制台打印输出。
photoObject:Photo (
key: "x2q0Asfjn2S8FYr6fIvA",
storeId: "",
imgName: "locator/stores/egdupo/BitoysVulcanizingandTireShop_rJTM7alkTp_M6L.jpg",
imageData: nil,
title: "",
description: "",
sort: 1,
uploadedAt: "",
createdAt: ""
)
有人吗?
提前谢谢你。
【问题讨论】:
-
请在您的问题中仅包含相关代码。阅读minimal reproducible example,然后阅读edit 您的问题,其中显示了演示问题的最小代码。
-
您是否检查过 createdAt 属性实际上包含您的日期字符串?
-
在 for document... 循环中,如果您添加 print(document) 或在 var photoObject... 然后print(photoObject),输出是什么?您能否在问题中复制并粘贴一个文档或对象的输出(作为文本!),以便我们查看对象中存储的内容?
-
我检查了 createdAt 属性包含 firebase 中的时间戳,而源代码属性包含字符串。 @andlin
-
photoObject:Photo(key: "x2q0Asfjn2S8FYr6fIvA", storeId: "", imgName: "locator/stores/egdupo/BitoysVulcanizingandTireShop_rJTM7alkTp_M6L.jpg", imageData: nil, title: "", description: "" , 排序: 1, uploadAt: "", createdAt: "") @Jay
标签: ios swift xcode firebase firebase-realtime-database