我尝试在UICollectionViewCell中显示转换为字符串的NSDate时遇到此错误,这有点奇怪。在viewDidLoad中,我从解析中获取对象并将它们存储在名为vaccineData的NSMutableArray中(在调试之后,它成功)。然后在cellForItemAtIndexPath中我有以下内容:
let myCell: VaccineCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("vaccineIdentifier", forIndexPath: indexPath) as! VaccineCollectionViewCell
//Document Images
let vaccinePost = self.vaccineData.objectAtIndex(indexPath.row) as! PFObject
if let imagesPosting: PFFile = (vaccinePost["documentImage"] as! PFFile) {
imagesPosting.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
if (error == nil) {
let image:UIImage = UIImage(data: imageData!)!
myCell.dogumentImage.image = image as UIImage
}
})
}
//Uploaded Date
let datePost = self.vaccineData.objectAtIndex(indexPath.row) as! PFObject
if let date:NSDate = (datePost["updatedAt"] as! NSDate) {
let dateformatter = NSDateFormatter()
dateformatter.dateStyle = NSDateFormatterStyle.LongStyle
dateformatter.timeStyle = NSDateFormatterStyle.NoStyle
let updateString = dateformatter.stringFromDate(date)
myCell.uploadedTime.text = updateString
print("Uploaded Time: \(updateString)")
} else {
print("There was an error")
}
return myCell
我的应用程序崩溃了:
if let date:NSDate = (datePost["updatedAt"] as! NSDate)
它甚至不会给我其他打印错误声明。我知道图像代码工作正常bc我已经注释掉了NSDate代码并显示了图像。我已经仔细检查了关键字拼写,代表,数据源,但无法找到这里为什么或为什么这是零。如果我错过任何东西,有人可以发现吗?提前谢谢!
答案 0 :(得分:1)
至于为什么没有达到else块 - 因为你强制向下转换,而不是选择(不知道确切的术语)。正确的代码读取语法是:
if let date = datePost["updatedAt"] as? NSDate {
} else {
print("There was an error")
}
至于为什么updatedAt
实际上没有设置 - 不知道,可能是因为你还没有更新条目。但是你应该简单地使用属性 updatedAt
来代替:
if let date = datePost.updatedAt {
} else {
print("There was an error")
}