Swift - Error: unexpectedly found nil while unwrapping an optional value

前端 未结 2 1037
悲&欢浪女
悲&欢浪女 2021-01-01 08:14

I have been making an app in Swift but I keep getting an error in my TableViewController class. I could not find any way to fix this and kept getting this error :



        
相关标签:
2条回答
  • 2021-01-01 08:57

    To avoid this Error, must have to make sure one importing thing while declaration of Object :

    var isActive: Bool?
    

    ? is the important to add, so now you can check nil values whenever you are, accessing this variable named isActive like below:

    1. if isActive == nil
    2. if let _isActive = isActive as Bool?

    So Optional values must be declared with the ?.

    0 讨论(0)
  • 2021-01-01 09:00

    It looks as if objects is coming back as nil. First check for nil:

    if !error {
        if let actualObjects = objects {
            let possibleUser = (actualObjects as NSArray).lastObject as? PFUser
            if let user = possibleUser {
                cell.usernameLabel.text = user.username
                // ...
            }
        }
    }
    

    Note: I changed your as to as?. lastObject is already returning an Optional and so you might as well let the execution continue if the last object cannot be converted to PFUser. Also, because lastObject might return nil, you also need to check that for nil.

    0 讨论(0)
提交回复
热议问题