The full error is typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))
I have tried both [QuoteData].self and QuoteData.self
func parseJSON(quoteData: Data) {
				let decoder = JSONDecoder()
				do {
						let decodedData = try decoder.decode(QuoteData.self, from: quoteData)
						//print(decodedData.zero[0].content)
						print(decodedData.zero[0].content)
				} catch {
						print(error)
				}
		}
Here is my struct
struct QuoteData: Decodable, Equatable {
		
		private enum CodingKeys : String , CodingKey { case zero = "0" }
		
		let zero : [Zero]
		
		
		struct Zero: Decodable, Equatable {
				let content : String
		}
}
This is the JSON I am requesting
[1 item
0: {
"content":"If you are out to describe the truth, leave elegance to the tailor."
"author":"Albert Einstein"
"category":"truth"
}
]
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I am building an app that will display a new quote every 24 hours. I am using API requests and Notification Center. I save the current/most recent quote fetched in User Defaults.
I am wanting to call the fetch quote function every 24 hours. What code technique would you suggest?
Topic:
Programming Languages
SubTopic:
Swift
Tags:
Swift
Notification Center
Network
Background Tasks
I created a UILabel programmatically and added it to my view controller. The view controller is enclosed in a PageViewController.
The line "Main Thread Checker: UI API called on a background thread: -[UIView init]" is showing on line 4(see below).
I haven't seen this error before. I have never had to add a label to a view on the main thread so I am not sure why this message is showing. Here is my code so far.
1. class QuizViewController: UIViewController {
2.
3. let questionLabel : UILabel = {
4. let label = UILabel()
5. label.translatesAutoresizingMaskIntoConstraints = false
6. label.textAlignment = .center
7. label.font = .systemFont(ofSize: 18, weight: .bold)
8. label.numberOfLines = 0
9. return label
10. }()
11.
12. var stackView : UIStackView = {
13. var stackView = UIStackView()
14. stackView.axis = .vertical
15. stackView.translatesAutoresizingMaskIntoConstraints = false
16. stackView.distribution = .fillEqually
17. stackView.spacing = 20
18. return stackView
19. }()
20.
21. var score = 0
22. var tag = 0
23.
24. var question : String
25. var answers : [String]
26.
27. init(question : Question) {
28. self.question = question.question
29. self.answers = question.incorrectAnswers
30. self.answers.append(question.correctAnswer)
31. super.init(nibName: nil, bundle: nil)
32. }
33.
34. required init?(coder: NSCoder) {
35. fatalError("init(coder:) has not been implemented")
36. }
37.
38. override func viewDidLoad() {
39. super.viewDidLoad()
40.
41.
42.
43. self.configureUI()
44. self.setup()
45.
46. }
47.
48. func configureUI() {
49. questionLabel.text = question
50. for answer in answers {
51. let button = UIButton()
52. button.setTitle(answer, for: .normal)
53. button.layer.cornerRadius = 20
54. button.layer.borderColor = UIColor.systemRed.cgColor
55. button.titleLabel?.numberOfLines = 0
56. button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
57. button.tag = tag
58. tag += 1
59. button.backgroundColor = .secondarySystemFill
60. stackView.addArrangedSubview(button)
61. }
62. }
63.
64. private func setup() {
65. view.addSubview(questionLabel)
66. view.addSubview(stackView)
67. view.backgroundColor = .systemBackground
68. NSLayoutConstraint.activate([
69. questionLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 80),
70. questionLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
71. questionLabel.heightAnchor.constraint(equalToConstant: 80),
72. questionLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
73.
74. stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
75. stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
76. stackView.heightAnchor.constraint(equalToConstant: 400),
77. stackView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20)
78. ])
79. }