I have been trying to find a struct inside the list of nested structs. Can anyone help me on this?
struct Places: PlacesProtocol {
private(set) var id: String
private(set) var name: String
private(set) var childPlaces: [PlacesProtocol]?
init(json: JSON) {
self.name = json[“Name”]
self.id = json[“Id”]
self. childPlaces = json[“ChildPlaces”].arrayValue.map { Places(json: $0) }
}
JSON:
{
"Id": "1",
"Name": "Place 1",
"ChildPlaces": [{
"Id": "12",
"Name": "Place 2",
"ChildPlaces": [{
"Id": "123",
"Name": "Place 3",
"ChildPlaces": [{
"Id": "1234",
"Name": "Place 4",
"ChildPlaces": null
}]
}, {
"Id": "13",
"Name": "Place 5",
"ChildPlaces": null
}]
}]
}
I have tried this:
nestedStruct.filter { $0.id == "13" }
I am able to parse this JSON in to the nested structure and I am trying to find a struct with Id. I have tried filter but it just filters only the first layer of the nested struct. Is there a way I can build recursive filter to find the struct which is deep inside the nested struct.
private(set)for? If you want a constant declare the members aslet