How do I safely unpack optionals (AlamoFire response object) in Swift

Jordan

I am using the below code to download data from a server. According to Crashlytics, we see a crash occurred (EXC_BREAKPOINT) on the conditional evaluation (the 'if' statement). I suspect it is because the code unpacking the optional member "statusCode" - I am new to Swift (10 years doing Obj-C) - and I am not certain what the best, safest way is to unpack this variable without causing a crash.

Note that this app its using SwiftyJSON, though I do not think that is relevant.

    Alamofire.request(url).responseJSON { (response) in
        if (response.response?.statusCode)! >= 200 && (response.response?.statusCode)! < 300
James Zaghini
Alamofire.request(url).validate().responseJSON { response in
    switch response.result {
    case .success(let json):
        // do something with json
    case .failure(let error):
        // handle error
    }
}

The validate() method replaces your line checking the statusCode. It defaults to using acceptableStatusCodes which are 200..<300.

I think that's the best way to handle this specific case.

For more general cases, you should avoid force unwrapping. Unwrap the optional using guard or if let. The Swift docs explain that in detail.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How Do I Safely Scan for Integer Input?

In Swift, how do I avoid both optionals and nil object references?

How do you unwrap Swift optionals?

How can I safely intercept the Response stream in a custom Owin Middleware

Swift: why do I need optionals anyways?

How do I unpack a single git object?

How can I print the json content of the response from post request in Alamofire in swift?

How do I check if an object is a collection? (Swift)

How can I safely retrieve this property of this object?

How to safely unpack dict in python?

Can you safely unwrap nested optionals in swift in one line?

How do I (safely) send a Python object to my Flask API?

How do I safely delay a web response in ASP.net?

Postgres: How do I safely remove a replica?

How to handle Alamofire Response in ios swift 4?

How do I get Data value from Combine + Alamofire Error response?

How do I share class members, safely?

How do I safely JSON.stringify?

How do I safely uninstall gnupg?

How Do I append CoreData Object with Swift?

What's a concise way to safely deal with implicitly unwrapped optionals in Swift?

wait for response Alamofire swift

How do I safely run memcached?

How can I use Alamofire to add auth header to each request, and do something if the response is 401?

How to catch the error when I upload the object using Alamofire - Swift 3?

How do I safely remove gwibber?

How do I return safely unwrapped optionals which are outside of their scope?

How do I unpack tuple format in R?

How do I safely access an object that has a union type with a non-literal key?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive