Getting fatal error: finding nil while unwrapping an Optional iOS Swift
Getting fatal error: finding nil while unwrapping an Optional iOS Swift
这对我来说有点奇怪,我在尝试显示 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)
它甚至不会给我 else 打印错误语句。我知道图像代码工作正常,因为我已经注释掉了 NSDate 代码并且它显示了图像。我已经仔细检查了关键字拼写、委托、数据源,但无法找到这是零的位置或原因。如果我遗漏了什么,有人能发现吗?提前致谢!
至于为什么没有到达else-block - 因为你force downcast, not optional(不知道确切的术语)。正确的代码阅读语法是:
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")
}
这对我来说有点奇怪,我在尝试显示 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)
它甚至不会给我 else 打印错误语句。我知道图像代码工作正常,因为我已经注释掉了 NSDate 代码并且它显示了图像。我已经仔细检查了关键字拼写、委托、数据源,但无法找到这是零的位置或原因。如果我遗漏了什么,有人能发现吗?提前致谢!
至于为什么没有到达else-block - 因为你force downcast, not optional(不知道确切的术语)。正确的代码阅读语法是:
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")
}