Ваш ответ API не String
, это Dictionary
Словарь в json - {}
Ответ API:
{
quote: "Travel is the most private of pleasures. There is no greater bore than the travel bore. We do not in the least want to hear what he has seen in Hong Kong.",
author: "Vita Sackville-West",
cat: "travel"
}
так что вы не будете проходить это условие в этой строке if let data = Json as? NSString
, потому что данные Dictionary
не String
Правильное решение:
let tasks = URLSession.shared.dataTask(with: URL(string: "https://talaikis.com/api/quotes/random/")!) { (data, response, error) in
if error != nil {
print("error")
} else {
if let content = data {
do {
let Json = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
if let data = Json as? [AnyHashable:Any] {
if let quote = data["quote"] as? String {
print(quote)
}
if let author = data["author"] as? String {
print(author)
}
if let cat = data["cat"] as? String {
print(cat)
}
DispatchQueue.main.async {
self.myLabel.text = "\(quote)"
}
}
} catch {
}
}
}
}
tasks.resume()
}