Or parameterized API call crashing in Swift

234 Views Asked by At

I am making a call to an API where I want have status equal to final or in progress. Here is the call I am using:

let request = NSMutableURLRequest(url: NSURL(string: "https://sportspage-feeds.p.rapidapi.com/games?status=in%20progress||status=final")! as URL,
                                                cachePolicy: .useProtocolCachePolicy,
                                            timeoutInterval: 10.0)

It works perfectly in Postman, but when trying it in my app it is crashing with this error:

Fatal error: Unexpectedly found nil while unwrapping an Optional value: file

Is there a different way to use or in Swift?

1

There are 1 best solutions below

3
Paulw11 On

You have manually percent encoded the space, but you have not percent encoded the two pipe characters. As a result the initialiser for NSURL fails, returning nil. Since you have force-unwrapped this value, your app then crashes.

You can use the function .addingPercentEncoding(withAllowedCharacters:) to percent encode a string appropriately and then create a URL.

Both percent encoding a string and creating a URL can fail, so these operations return an optional. You should use conditional unwrapping rather than force unwrapping to avoid crashes if these operations fail.

Many NS classes have bridged Swift equivalents including URLRequest for NSURLRequest and URL for NSURL. Idiomatic Swift eschews the use of an NS class when a Swift equivalent exists.

Use something like

if let urlStr = "https://sportspage-feeds.p.rapidapi.com/games?status=in progress||status=final".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), let url = URL(urlStr) {
    let request = URLRequest(url, timeoutInterval: 10)
    ...
}   

As Matt pointed out in a comment, the correct way to construct a URL in iOS is to use URLComponents. This allows you to specify each component of the URL independently and not worry about things like manual percent encoding.

The use of URLComponents is particularly important where you collect input from the user and they could attempt to manipulate the resulting URL string.

var components = URLComponents()
components.scheme = "https"
components.host = "sportspage-feeds.p.rapidapi.com"
components.path = "/games"
components.queryItems = [(URLQueryItem(name:"status", value:"in progress||status=final"))]

if let url = components.url {
    let request = URLRequest(url, timeoutInterval: 10)
    ...
}