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)
...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…