-3

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!)
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
Appu
  • 3
  • 1
  • 3
  • on which line you are getting error? have to check by applying exception break point? – aBilal17 Jun 29 '18 at 11:05
  • 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 '18 at 11:09
  • `response.error` is nil, and therefore `response.error!` crashes ... – Martin R Jun 29 '18 at 11:11
  • @aBilal17, Crash is not occurred at `json as Any`, crash occurred at `response.error!`. – Kuldeep Jun 29 '18 at 11:11
  • 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 '18 at 11:15
  • @aBilal17 This line completionBlock(json as Any, response.error!) – Appu Jun 29 '18 at 11:19
  • ok,, just update this line by this new line and then check. completionBlock(json as Any, "No Error found") – aBilal17 Jun 29 '18 at 11:26

1 Answers1

-2

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
                   }
        }
    }
    }
Rakesh Patel
  • 1,673
  • 10
  • 27
  • 3
    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 '18 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 '18 at 11:17
  • @Appu, you need to change your `completionBlock` param. `Error` to `String` than this code will work. – Kuldeep Jun 29 '18 at 11:20
  • make as --> typealias CompletionBlock = ( _ response : Any , _ error : String) -> Void – Rakesh Patel Jun 29 '18 at 11:26
  • Thanks @Kuldeep its working fine – Appu Jun 29 '18 at 11:27
  • thanks @RakeshPatel – Appu Jun 29 '18 at 11:28
  • Hi @Rakesh Patel I have one more question here i am checking if ((userName != nil) && (password != nil)){ }. i am getting this warning "Comparing non-optional value of type 'String' to nil always returns true" how to fix this line. – Appu Jun 29 '18 at 11:32
  • might be your username or password has nonoptional value...can you update your code in question where you set username and password? – Rakesh Patel Jun 29 '18 at 11:36
  • Please check my updated answer...-->login(userName: String?, password: String? – Rakesh Patel Jun 29 '18 at 11:37
  • Yes it's working, Thank you so much @RakeshPatel – Appu Jun 29 '18 at 11:40
  • Hi @RakeshPatel i have one more doubt please solve this one, I set completionBlock in failer case. But its showing error "Expression type 'Void' is ambiguous without more context". – Appu Jun 29 '18 at 11:55
  • How you set completion block? can you show me? – Rakesh Patel Jun 29 '18 at 12:02
  • @RakeshPatel typealias CompletionBlock = ( _ response : Any , _ error : String) -> Void This is my completionBlock – Appu Jun 29 '18 at 12:15
  • i mean what you pass as parameter in completionblock in failure case – Rakesh Patel Jun 29 '18 at 12:21
  • i think you should pass --> completionBlock("No Data found", error) – Rakesh Patel Jun 29 '18 at 12:33
  • completionBlock(nil as Any, "Some errors are found") – Appu Jun 29 '18 at 12:33
  • Now its working fine, Thanks Rakesh. completionBlock("No Data found", "Some errors are found") – Appu Jun 29 '18 at 12:36
  • thats great...!!. I advise you to do clear a concept of optional and nonoptional value in swift. – Rakesh Patel Jun 29 '18 at 12:40
  • Yeah, Thank you so much – Appu Jun 29 '18 at 12:53