0

Hi Im using Sinch sms verification to sign up users in my app but after updating my code to swift 3 (and sinch sdk currently 2.0.3), Im getting the following error

Cannot convert value of type '(Bool, NSError?) -> Void' to expected argument type '(Bool, Error?) -> Void'

I know that if I change NSError to Error , this error disappear

Sinch Verification Swift 3

but I need to get the error code from the NSError to alert my user on what happened depend on this error

   if (result.success){
          //login user
         } else {
            if error!.code == 6 {
                   let msg = NSLocalizedString("This Phone Number is invalid",
                         comment: "This Phone Number is invalid pop up alert")
                   print(msg)
            }
   } 

From Sinch Docs , Im suppose to use this NSError:

https://www.sinch.com/docs/verification/ios/#swiftsdk

 let verification = SMSVerification(applicationKey:"<APP KEY>", phoneNumber: phoneNumberInE164)
    verification.initiate { (result: InitiationResult, error: NSError?) -> Void in
        // handle outcome
    }

Is there a way to get the error.code from error:Error? Thanks for your help

Community
  • 1
  • 1
jerem
  • 1,016
  • 2
  • 12
  • 27
  • 2
    Swift 3 bridges all `NSError` from Objective C code to `Error` type(protocol). So in closure parameters you should use `Error` and convert it inside closure to `NSError` using `as`. – user28434'mstep Dec 13 '16 at 11:47

1 Answers1

3
let verification = SMSVerification(applicationKey:"<APP KEY>", phoneNumber: phoneNumberInE164)
    verification.initiate { (result: InitiationResult, error: Error?) -> Void in
        // handle outcome
        if (result.success){
          //login user
         } else if let error = error as? NSError, error.code == 6 {
            let msg = NSLocalizedString("This Phone Number is invalid",
                         comment: "This Phone Number is invalid pop up alert")
            print(msg)
         }
    }
zombie
  • 5,069
  • 3
  • 25
  • 54
  • thanks @zombie , if you have some piece of code for this second question it would be great : http://stackoverflow.com/questions/41120985/sinch-sms-verification-2-0-3-sdk-swift-3 – jerem Dec 13 '16 at 12:33