-2

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?

ShedSports
  • 541
  • 2
  • 7
  • 14

1 Answers1

0

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)
    ...
}
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • No, should use URLComponents. – matt Apr 06 '21 at 01:57
  • tried it but keep getting this error: ```No value associated with key CodingKeys``` – ShedSports Apr 06 '21 at 02:38
  • That's nothing to do with the code in this question (or answer) - This is an error from your use of `Codable` for JSON decoding. As the Magic 8 Ball says, "Ask again" - If you can't find the answer by searching for information on decoding JSON in Swift with Codable, ask a new question showing the relevant code – Paulw11 Apr 06 '21 at 02:39