NSData: unexpectedly found nil while unwrapping an Optional value

前端 未结 3 480
攒了一身酷
攒了一身酷 2021-01-29 07:47

It may be the basic swift quetion, But i am new to swift or iOS development. I am getting the error fatal error: unexpectedly found nil while unwrapping an Optional value<

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-29 08:31

    You declared nilObj as optional and initialised it with nil. Then in your else clause you are trying to unwrap that. For fixing the issue you just need to remove that !

    Change your code to:

    func Call() -> NSData?
    {        
         let nilObj: NSData? = nil 
         if(false)
         {  
            // Doing something     
         }
         else
         {         
             return nilObj       
         } 
    }
    

提交回复
热议问题