T
T
The_XXI2021-03-26 13:13:39
Swift
The_XXI, 2021-03-26 13:13:39

Why is there an error on URLRequest?

There is this code:

import Cocoa

var request = URLRequest(url: URL(string: "https://finnhub.io/api/v1/index/constituents?symbol=^DJI&token=c16aimn48v6ppg7elmog")!)

let task = URLSession.shared.dataTask(with: request) { data, response, error in
print(String(decoding: data!, as: UTF8.self))
}
task.resume()

It throws an error: Fatal error: Unexpectedly found nil while unwrapping an Optional value: file RequestLesson.playground, line 3
Why is this happening?

When replacing the address, for example, with such " https://finnhub.io/api/v1/stock/profile2?symbol=AA... " the request is processed and the data is displayed

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
briahas, 2021-03-26
@The_XXI

Most likely
URL(string: " https://finnhub.io/api/v1/index/constituents?symbo... ")!
not converted to URL.
Move the url to a separate variable and see what this variable will be equal to

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question