[英]I am getting nil while parsing JSON with Swift
I am learning Swift and I am studying for parsing JSON.我正在学习 Swift,我正在学习解析 JSON。 I have a little complicated API for me and I can't get data from it.我的 API 有点复杂,我无法从中获取数据。 Can you let me know what mistakes I do below?你能告诉我我在下面犯了什么错误吗?
import UIKit
struct DataSource: Codable {
let status: Int
let totalResults: String
let articles: [Articles]
}
struct Articles: Codable {
let source: [Source]
let author: String
let title: String
}
struct Source: Codable {
let name: String
}
let source = URL(string: "http://newsapi.org/v2/everything?q=bitcoin&from=2020-03-29&sortBy=publishedAt&apiKey=51480e6fd4294010d")!
URLSession.shared.dataTask(with: source) { (data, response, error ) in
if let data = data {
let publishing = try? JSONDecoder().decode([DataSource].self, from: data)
print(publishing)
}
}.resume()
I fixed.我解决了。 Thank u for your all recommandations.谢谢你的所有建议。 Appreciated!赞赏!
import UIKit
class ViewController: UIViewController { class 视图控制器:UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
struct DataSource: Codable {
let status: String?
let totalResults: Int?
let articles: [Articles]
}
struct Articles: Codable {
let source: Source
let author: String
let title: String
}
struct Source: Codable {
let id: String?
let name: String
}
let source = URL(string: "http://newsapi.org/v2/everything?q=bitcoin&from=2020-03-30&sortBy=publishedAt&apiKey=XXXXXX")!
URLSession.shared.dataTask(with: source) { (data, response, error ) in
do
{
let publishing = try JSONDecoder().decode(DataSource.self, from: data!)
print(publishing)
}
catch
{
print(error)
}
} .resume()
}
} }
I make this example on playground我在操场上做这个例子
import UIKit
import PlaygroundSupport
// MARK: - ProfileResponse
struct ArticlesResponse: Codable {
var status: String
var totalResults: Int
var articles: [Article]
}
// MARK: - ProfileArticle
struct Article: Codable {
var source: Source
var author: String?
var title, articleDescription: String
var url: String
var urlToImage: String?
var publishedAt: String
var content: String?
enum CodingKeys: String, CodingKey {
case source, author, title
case articleDescription = "description"
case url, urlToImage, publishedAt, content
}
}
// MARK: - ProfileSource
struct Source: Codable {
var id: String?
var name: String
}
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let label = UILabel()
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.text = "Hello World!"
label.textColor = .black
view.addSubview(label)
self.view = view
if let url = URL(string: "http://newsapi.org/v2/everything?q=bitcoin&from=2020-03-30&sortBy=publishedAt&apiKey=f0b7d2e59beb4bcdb7a737a25c3542bc") {
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
do {
let decoder = JSONDecoder()
let data = try decoder.decode(ArticlesResponse.self, from: data)
label.text = "\(data.status)"
} catch let error {
print(error)
}
}
}.resume()
}
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.