0

I am trying to add and image to a collectionView cell if a file associated with the cell is located on the device.

The file is there so the below code unhides the image, however i get an error that it found nil trying to unwrap optional.

any ideas whats wrong with the code?

enter image description here

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell: JourneyCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! JourneyCollectionViewCell
    
    // query if file is on LDS and add image to indicate
    let cellPartName = self.partArray[indexPath.item].name
    let checkQuery = PFQuery(className: "downloadedAudio")
        checkQuery.whereKeyExists(cellPartName)
        checkQuery.fromLocalDatastore()
        checkQuery.getFirstObjectInBackground(block: { (object, error) in
            if error != nil || object == nil {
                print("The file does not exist locally on the device, hide the image.")
                //cell.ImageDownloaded.image = UIImage(named: "")

                // crashes on this line
                cell.ImageDownloaded.isHidden = true
            } else {
                print("the file already exists on the device, show the image.")
                //cell.ImageDownloaded.image = UIImage(named: "download")

                // crashes on this line
                cell.ImageDownloaded.isHidden = false
            }
        })


    return cell
    
}




the file already exists on the device, show the image.
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)

image "download" is in the cassette.

Community
  • 1
  • 1
Pippo
  • 1,439
  • 1
  • 18
  • 35
  • Where is `cell` defined? – BallpointBen Jan 22 '17 at 23:50
  • What does the crash say? Most details on that would be useful. – raidfive Jan 22 '17 at 23:51
  • I don't seem any `cell` variable defined within your method. Have you forgotten to dequeue a cell? `let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath);` – ilbesculpi Jan 22 '17 at 23:55
  • Sorry i didn't copy the cell = line. details of crash at bottom of edit – Pippo Jan 23 '17 at 00:04
  • Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Robotic Cat Jan 23 '17 at 00:13

1 Answers1

2

One quick note. When declaring a variable you should always use camelCase. Therefore, ImageDownloaded should be imageDownloaded.

In the very few lines of code here, it seems that it crashes on this line:

cell.ImageDownloaded.isHidden = false

This means that the variable ImageDownloaded is probably the variable that is nil. In my experience, this might be due to the fact that you have the variable declared in the code of your cell class, but the associated UIImageView is not connected to the declaration. Thus from the storyboard, it looks like it exists, and from code it looks like it exists, but when you try to access it is suddenly breaks.

This may have happened if you deleted and then pasted either part back in. It looks the same, but the connection is no longer there. TO fix it, just control-drag again into the empty circle next to the code declaration.

Acoop
  • 2,586
  • 2
  • 23
  • 39
  • You are correct. i am a muppet and the link from interface builder to my class was broken. – Pippo Jan 23 '17 at 00:28