0

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] } 
1
  • 1
    Show an example (in your question, not in comment) of where/how you want to access the title property. Commented Jul 28, 2019 at 1:31

1 Answer 1

1

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.

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.