0

I can't get all elements of my array. I think the problem is in my Model, but i don't know how to correct it. The following JSON is given:

    {
    "statusCode": 200,
    "message": "Success",
    "content": [
        {
            "description": {
                "1": {
                    "name": "First",
                    "cost": 200,
                    "count": 1
                },
                "2": {
                    "name": "Second",
                    "cost": 500,
                    "count": 1
                }
            },
            "sum": 700,
            "created_at": "13.11.2018 17:22"
        }
    ]
}

My Model:

// MARK: - History
struct History: Codable {
    let statusCode: Int
    let message: String
    let content: [History_Content]
}

// MARK: - Content
struct History_Content: Codable {
    let description: [String: History_Description]
    let sum: Int
    let created_at: String

    enum CodingKeys: String, CodingKey {
        case description
        case sum
        case created_at
    }
}

// MARK: - Description
struct History_Description: Codable {
    let name: String
    let cost, count: Int
}

My cellForRowAt :

var arrData = [History_Content](). 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "history_cell", for: indexPath) as! HistoryTableViewCell
        
        cell.time.text = arrData[indexPath.row].created_at
        for i in arrData[indexPath.row].description.self{
            cell.desc.text = "You bought \"\(i.value.name)\""

        }
        
        return cell
    }

I just get a single element of description. How can i get all elements of this array? What do i need to change in my code?

8
  • 3
    description is dictionary not array ... Commented Jul 2, 2020 at 11:03
  • 1
    What do you do after the decoding, what do you assign to arrData? Commented Jul 2, 2020 at 11:04
  • Change let description: [String: History_Description] (dictionary) to let description: [History_Description] (array) Commented Jul 2, 2020 at 11:10
  • @Deitsch Bad advice since it's a dictionary Commented Jul 2, 2020 at 11:14
  • Deitsch, i tried, but it doesn't work Commented Jul 2, 2020 at 11:15

2 Answers 2

1

If you want to merge name of all values you can do

let row = arrData[indexPath.row]
cell.desc.text = "You bought \(row.description.values.map { $0.name }.joined(separator: ", "))"  

But if the order is important you can sort first, here I sort by key

let sorted = row.description.sorted(by: { $0.key < $1.key })
cell.desc.text = "You bought \(sorted.map { $0.value.name }.joined(separator: ", "))"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This code works like @erik_m_martens, but takes less characters
1
for i in arrData[indexPath.row].description.self{
   cell.desc.text = "You bought \"\(i.value.name)\""
}

You are iterating over each value of the description-dictionary and setting the contents to cell.desc.text. However each time you set the cell.desc.text in your for-loop you override the previously assigned value.

What do you want to achieve here for the cell.desc.text? A comma-separated list? Then you should do something like this:

var cellDescr = "You bought"
for i in arrData[indexPath.row].description.self {
    cellDescr.append(" \"\(i.value.name)\",")
}
cell.desc.text = cellDescr.trimmingCharacters(in: .punctuationCharacters) // remove dangling comma

Let me know if this helps or you want to achieve something else and I will update my answer for you.

1 Comment

Thank you so much. It is working That is what i needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.