How to solve Fatal error: Unexpectedly found nil while unwrapping an Optional value in swift 4 [duplicate]
How to solve Fatal error: Unexpectedly found nil while unwrapping an Optional value in swift 4 [duplicate]
This question already has an answer here:
I am new to a swift, please someone help/advise me.
And I am sending the request to a server and using completionBlock. When login button clicks the success completionBlock is calling and here i am getting struck.
This is my request
typealias CompletionBlock = ( _ response : Any , _ error : Error) -> Void
func login(userName: String, password: String, completionBlock: @escaping CompletionBlock ) -> Void
{
let parameter = ["gs_username":userName, "gs_password":password] as [String : AnyObject]
let url = "user-login"
let fullUrl = baseUrl?.appendingPathComponent(url)
if ((userName != nil) && (password != nil)) {
Alamofire.request(fullUrl!, method: .post, parameters: parameter, headers: nil)
.validate()
.responseJSON { response in
switch response.result{
case .success:
let json = response.result.value
completionBlock(json as Any, response.error!)
break
case .failure(let error):
print("getEvents error: (error)")
//completionBlock(nil as AnyObject, error)
break
}
}
}
}
This is the error line
completionBlock(json as Any, response.error!)
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
What is the value in json? if it is nill, change this line by completionBlock(json as? Any, response.error!) and then check
– aBilal17
Jun 29 at 11:09
response.error
is nil, and therefore response.error!
crashes ...– Martin R
Jun 29 at 11:11
response.error
response.error!
@aBilal17, Crash is not occurred at
json as Any
, crash occurred at response.error!
.– Kuldeep
Jun 29 at 11:11
json as Any
response.error!
Your
response.error
in .success
case is nil
(no error, connection is successful), and you're trying to explicitly unwrap that value. Change the error code to an empty string or some other appropriate type, and it should be ok.– SimpleBeat
Jun 29 at 11:15
response.error
.success
nil
1 Answer
1
Update as below
func login(userName: String?, password: String?, completionBlock: @escaping CompletionBlock ) -> Void
{
let parameter = ["gs_username":userName, "gs_password":password] as [String : AnyObject]
let url = "user-login"
let fullUrl = baseUrl?.appendingPathComponent(url)
if ((userName != nil) && (password != nil)) {
Alamofire.request(fullUrl!, method: .post, parameters: parameter, headers: nil)
.validate()
.responseJSON { response in
switch response.result{
case .success:
let json = response.result.value
completionBlock(json as Any, "No Error found")
break
case .failure(let error):
print("getEvents error: (error)")
//completionBlock(nil as AnyObject, error)
break
}
}
}
}
Answers are more helpful (to OP and to future readers) if they explain the problem and the solution, instead of dumping code only.
– Martin R
Jun 29 at 11:13
Hi @Rakesh Patel thanks for answering, But its not taking a string value and i changed this line completionBlock(json as Any, "No Error found" as! Error) . But i got error "Thread 1: signal SIGABRT" error and in console "Could not cast value of type 'Swift.String' (0x10781a9f8) to 'Swift.Error' (0x7f88708e5798). "
– Appu
Jun 29 at 11:17
@Appu, you need to change your
completionBlock
param. Error
to String
than this code will work.– Kuldeep
Jun 29 at 11:20
completionBlock
Error
String
make as --> typealias CompletionBlock = ( _ response : Any , _ error : String) -> Void
– Rakesh Patel
Jun 29 at 11:26
Thanks @Kuldeep its working fine
– Appu
Jun 29 at 11:27
on which line you are getting error? have to check by applying exception break point?
– aBilal17
Jun 29 at 11:05