-1

I have structure (Code below), i put there JSON values. How i can get image:URL for name: cafe from my struct?

for example:

  • name: "cafe" image: "url_link"
  • name: "bar" image: "url_link"
  • name: "sushi" image: "url_link"

My Struct

struct Root: Codable { let data: [InnerItem] } struct InnerItem:Codable { let id: Int? let image: String? let name: String? private enum CodingKeys : String, CodingKey { case id = "id", image = "image", name = "name" } } 
2
  • 1
    FYI - you don't need to define CodingKeys if the keys all match your property names. Commented Jul 17, 2018 at 18:05
  • … and – as already mentioned in your previous question – declare the struct members as non-optional (no question marks). Commented Jul 17, 2018 at 18:22

1 Answer 1

1

You can try

do { let decoder = try JSONDecoder().decode(Root.self, from: response.data!) let innerCafe = decoder.data.first(where: { $0.name == "cafe" }) } catch { print(error) } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.