I've got 2 JSON from different API.
This one (Activities):
{
"oldest": "2019-01-24T00:00:00+00:00",
"activities": [
{
"message": "<strong>Henrik</strong> didn't resist a guilty pleasure at <strong>Starbucks</strong>.",
"amount": 2.5,
"userId": 2,
"timestamp": "2019-05-23T00:00:00+00:00"
},
{
"message": "<strong>You</strong> made a manual transfer.",
"amount": 10,
"userId": 1,
"timestamp": "2019-01-24T00:00:00+00:00"
}
]
}
And this one (Users):
[
{
"userId": 1,
"displayName": "Mikael",
"avatarUrl": "http://qapital-ios-testtask.herokuapp.com/avatars/mikael.jpg"
},
{
"userId": 6,
"displayName": "Daniel",
"avatarUrl": "http://qapital-ios-testtask.herokuapp.com/avatars/daniel.jpg"
}
]
There a lot more activities and users inside but I removed them to make this shorter.
Now, the goal would be to have something like this: example.
I've set up a MainViewController
and a MainViewControllerCell
MainViewController:
struct Activities: Decodable {
let oldest: String
let activities: [Activity]
}
struct Activity: Decodable {
let message: String
let amount: Float
let userId: Int
let timestamp: String
}
struct Users: Decodable {
let avatarUrl: String
let displayName: String
let userId: Int
}
class MainTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// let userJSONURLString = "https://qapital-ios-testtask.herokuapp.com/users"
let activitiesJSONURLString = "https://qapital-ios-testtask.herokuapp.com/activities?from=2016-05-23T00:00:00+00:00&to=2019-05-23T00:00:00+00:00"
// guard let userURL = URL(string: userJSONURLString) else { return }
guard let activitiesURL = URL(string: activitiesJSONURLString) else { return }
URLSession.shared.dataTask(with: activitiesURL) { (data, response, err) in
guard let data = data else { return }
do {
// Activities
let activities = try JSONDecoder().decode(Activities.self, from: data)
print(activities)
// Users
// let users = try JSONDecoder().decode([Users].self, from: data)
// print(users)
} catch let jsonErr {
print("Error serializing json: ", jsonErr)
}
}.resume()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 0
}
MainControllerViewCell
class MainTableViewCell: UITableViewCell {
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var imgView: UIImageView!
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var amountLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
As you can see, I can already read the JSON. Now, I've never done this before so I don't know which is the best approach.
How would I connect the userId
from Activity
to the userId
from Users
?
How would I connect the amount
, message
, and the avatars to the labels?
Basically how do I populate the cells with the JSONs to have it like the example?