Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

swift get error from the from completion

let task = session.dataTask(with: urlRequest) {
  (data, response, error) in
  // check for any errors
  guard error == nil else {
    print("error calling GET on /todos/1")
    print(error!)
    return
  }
  // make sure we got data
  guard let responseData = data else {
    print("Error: did not receive data")
    return
  }
  // parse the result as JSON, since that's what the API provides
  do {
    guard let todo = try JSONSerialization.jsonObject(with: responseData, options: [])
      as? [String: Any] else {
      print("error trying to convert data to JSON")
      return
    }
    // now we have the todo
    // let's just print it to prove we can access it
    print("The todo is: " + todo.description)
    
    // the todo object is a dictionary
    // so we just access the title using the "title" key
    // so check for a title and print it if we have one
    guard let todoTitle = todo["title"] as? String else {
      print("Could not get todo title from JSON")
      return
    }
    print("The title is: " + todoTitle)
  } catch  {
    print("error trying to convert data to JSON")
    return
  }
}
task.resume()
Comment

swift get error from the from completion


func asynchronousWork(completion: (inner: () throws -> [String: Any]) -> Void) -> Void {
    URLConnection.sendAsynchronousRequest(request, queue: queue) { 
        (response, data, error) -> Void in
        guard let data = data else { return }
        do {
            guard let result = try JSONSerialization.JSONObjectWithData(data, options: []) 
                as? [String: Any] else {
                completion(inner: { throw OurError.InvalidJSONType })
            }
            completion(inner: { return result })
        } catch let error {
            completion(inner: { throw error })
        }
    }
}

// Call
asynchronousWork { (inner: () throws -> [String: Any]) -> Void in
    do {
        let result = try inner()
    } catch let error {
        print(error)
    }
}

Comment

swift get error from the from completion

let result = divide(10, by: 2) // Result<Int, DivisionError>
let mapSuccess = result.map { divide($0, by: 2) } // Result<Result<Int, DivisionError>, DivisionError>
let flatMapSuccess = result.flatMap { divide($0, by: 2) } // Result<Int, DivisionError>
let mapFailure = result.mapError { NSError(domain: $0.localizedDescription, code: 0, userInfo: nil) }
let flatMapFailure = result.flatMapError { .failure(NSError(domain: $0.localizedDescription, code: 0, userInfo: nil)) }
Comment

PREVIOUS NEXT
Code Example
Swift :: 95 dollars in rupees 
Swift :: Swift Function With Argument Label 
Swift :: Autoclosure Swift 
Swift :: dfghbghjjmyuhjtdcfbjj 
Swift :: swiftui button only text tappable 
Swift :: change button image tint color swift 
Swift :: Swift Find Number of Dictionary Elements 
Swift :: how to decode optional bool swift 
Swift :: struct exsmple 
Swift :: Swift Find Number of Array Elements 
Swift :: convert dictionary to data 
Swift :: swift split an array into chunks 
Swift :: swiftui orientation failed after change orientation popup 
Swift :: swift 5 macos close app 
Swift :: Swift Nil-coalescing operator 
Swift :: swift allow gesture recognizer over others 
Ruby :: ruby file extension 
Ruby :: find records created in a particular month rails 
Ruby :: get current year in ruby 
Ruby :: ruby print string 
Ruby :: run a specific migration rails 
Ruby :: ruby deep merge 
Ruby :: rails check if key exists 
Ruby :: rails hidden field default value 
Ruby :: singleton class in ruby 
Ruby :: ruby hash.each 
Ruby :: rails secure uuid 
Ruby :: font awesome icon rails submit button 
Ruby :: how works httparty ruby 
Ruby :: ruby check if hash has method before calling it 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =