Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

swift create a method who can return result or throw an error

class SecurityService {
  static func loginWith(email: String, password: String, callback: (error: NSError?, user: User?) -> Void) -> Void {

    let URL = ".."
    let params = [
      "email": email,
      "password": password
    ]

    HTTP.request(URL, method: "POST", params: params) { (error, result) in
      if let error = error {
        callback(error: error, user: nil)
      } else {
        guard let JSON = result else {
          let someDomain = "some_domain"
          let someCode = 100
          let someInfo = [NSLocalizedDescriptionKey: "No results were sent by the server."]

          let error = NSError(domain: someDomain, code: someCode, userInfo: someInfo)
          callback(error: error, user: nil)
          return
        }

        guard let name = JSON["name"] as? String, email = JSON["email"] as? String else {
          let someDomain = "some_domain"
          let someCode = 100
          let someInfo = [NSLocalizedDescriptionKey: "No user properties were sent by the server."]

          let error = NSError(domain: someDomain, code: someCode, userInfo: someInfo)
          callback(error: error, user: nil)
          return
        }

        let user = User(name: name, email: email)
        callback(error: nil, user: user)
      }
    }
  }
}
Comment

PREVIOUS NEXT
Code Example
Swift :: how to insert element at start of the array ios swift 
Swift :: and in swift 
Swift :: swift ui enum 
Swift :: swift change background color 
Swift :: listview swiftui 
Swift :: swift url request 
Swift :: swift string concatenation 
Swift :: swift navigation bar title color 
Swift :: swift alert toast 
Swift :: change font swiftui 
Swift :: swift create uinavigationcontroller programmatically 
Swift :: swift simulatore condition 
Swift :: swft ui image 
Swift :: ios get device id 
Swift :: Swift Remove an Element from an Array 
Swift :: swiftui tabview background color 
Swift :: response.result.value alamofire 5 
Swift :: replace back button image swift 
Swift :: Swift guard-let Statement 
Swift :: swift array map example 
Swift :: Swift Right Shift Operator 
Swift :: after redirect from another viewcontroller in swift 5 make full view view controller 
Swift :: Logical Operators Swift 
Swift :: swift singleton 
Swift :: swift core data order by 
Swift :: swift integer 
Swift :: image copy swift extension 
Swift :: bzxjhjgvjgvjgvjv 
Swift :: redux trong swift 
Swift :: swift modify dictionary 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =