Search code examples
swiftbooleanuilabelhidden

Not sure why I'm getting an error when setting a UILabel as hidden | Swift


I have a function that checks if the user has access to the photo library. I am trying to set a PhotoAccessLabel as hidden. I am getting the error:

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

I am new to Swift and am simply editing a file created by a peer. Any tips on what is going wrong?

func photoPermissionAccepted() {
    DispatchQueue.main.async {
        if self.allassets == nil || self.allassets.count <= 0 {
            DispatchQueue.main.async {
                self.permissionLabel.isHidden = false
            }
            self.PhotoAccessLabel.isHidden = true

        } else {
            DispatchQueue.main.async {
                self.permissionLabel.isHidden = true
            }
        }

    }
}
func photoPermissionDenied() {
    DispatchQueue.main.async {
        self.PhotoAccessLabel.isHidden = false
    }
}

Solution

  • The fatal error: unexpectedly found nil while unwrapping an Optional value appears the most when you have declared an @IBOutlet, but not connected to the storyboard. Please check both the outlet connections for PhotoAccessLabel & permissionLabel.