简体   繁体   English

发生致命错误:展开可选的iOS Swift时找到nil

[英]Getting fatal error: finding nil while unwrapping an Optional iOS Swift

This is a little strange to me that I'm getting this error when trying to show an NSDate converted to a string in a UICollectionViewCell. 这让我有些奇怪,在尝试显示将NSDate转换为UICollectionViewCell中的字符串时遇到此错误。 In viewDidLoad I am grabbing objects from parse and storing them in an NSMutableArray called vaccineData (which after debugging, is successful). 在viewDidLoad中,我从解析中获取对象,并将其存储在称为疫苗数据的NSMutableArray中(在调试后成功)。 Then in cellForItemAtIndexPath I have the following: 然后在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

My app crashes on the line: 我的应用程序崩溃了:

if let date:NSDate = (datePost["updatedAt"] as! NSDate)

It won't even give me the else print error statement. 它甚至都不会给我else打印错误声明。 I know the image code is working correctly bc I've commented out the NSDate code and it showed the image. 我知道图像代码可以正常工作,因为我已经注释掉了NSDate代码并显示了图像。 I've double checked keyword spellings, delegates, datasources but can't find where or why this is nil. 我已经仔细检查了关键字的拼写,委托,数据源,但找不到在哪里或为什么没有。 Can someone spot if I'm missing anything? 如果我缺少任何东西,有人可以发现吗? Thanks in advance! 提前致谢!

As to why the else-block is not reached - because you force downcast, not optionally (do not know the exact terminology). 至于为什么未达到else-block的原因-因为您强制向下转换,所以不选择(不知道确切的术语)。 The correct code reading syntax would be: 正确的代码读取语法为:

if let date = datePost["updatedAt"] as? NSDate {

} else {
    print("There was an error")
}

As to why the updatedAt is not actually set - no idea, maybe because you did not update the entry yet. 至于为什么未真正设置updatedAt不知道,可能是因为您尚未更新条目。 But you should probably simply use the property updatedAt instead anyway: 但是无论如何,您可能应该只使用属性 updatedAt代替:

if let date = datePost.updatedAt {

} else {
    print("There was an error")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS Swift登录错误:致命错误:展开可选值时意外发现nil - Login error with iOS Swift: fatal error: unexpectedly found nil while unwrapping an Optional value Swift:致命错误:在展开可选值时意外发现nil - Swift : fatal error: unexpectedly found nil while unwrapping an Optional value Swift-致命错误:解开Optional值时意外发现nil - Swift - Fatal error: unexpectedly found nil while unwrapping an Optional values 快速致命错误:解开Optional值时意外发现nil - swift fatal error: unexpectedly found nil while unwrapping an Optional value Swift致命错误:解开Optional值时意外发现nil - Swift fatal error: unexpectedly found nil while unwrapping an Optional value 致命错误:在Swift 3中解开Optional值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value in Swift 3 SWIFT-致命错误:解开可选值时意外发现nil - SWIFT - fatal error: unexpectedly found nil while unwrapping an Optional value UITableViewDelegate无法正常工作并收到错误消息(致命错误:在swift 4的UITableView中展开Optional值时意外发现nil) - UITableViewDelegate not working getting an error (Fatal error: Unexpectedly found nil while unwrapping an Optional value in UITableView in swift 4) iOS Swift致命错误:展开可选值时意外发现nil - IOS Swift fatal error: unexpectedly found nil while unwrapping an Optional value Swift / iOS 8:搜索栏引发致命错误:在展开可选值时意外发现nil - Swift/iOS 8: search bar raising fatal error: unexpectedly found nil while unwrapping an Optional value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM