Reputation: 128
I am learning how to access property in struc. I am trying to access the title property from the categorie struct. I am only able to to business.categories. How can I do to access the title property.
struct categorie: Decodable{
let title: [String]
}
struct Business: Decodable {
let name: String = ""
let categories: [categorie]
}
Upvotes: 0
Views: 101
Reputation: 16715
First thing's first. Let's fix your struct names. struct
declarations should be CapitalizedCamelCase
, so your struct looks like this:
struct Categorie: Decodable{
let title: [String]
}
struct Business: Decodable {
let name: String = ""
let categories: [Categorie]
}
Next, you're looking for the title property of an element in the array of categories
, so you'd do it like so:
business.categories[0].title
You need to specify which element of the array you want to examine. In the example above, I'm getting the 1st element's ([0]
) title
property. You'll want to put some logic in to protect against categories being empty.
Upvotes: 1