2

I've tried following this and this and this, but I'm having issues getting the data from a string array.

I have my state set as

 businessHours = : [
    "Monday: 7:00 AM – 7:00 PM",
    "Tuesday: 7:00 AM – 7:00 PM",
    "Wednesday: 7:00 AM – 7:00 PM",
    "Thursday: 7:00 AM – 7:00 PM",
    "Friday: 7:00 AM – 7:00 PM",
    "Saturday: 7:00 AM – 7:00 PM",
    "Sunday: Closed"
 ]

This only gets me the first element from the array

ForEach(businessHours.indices) {
    Text(self.businessHours[$0])
}

This fails

ForEach(businessHours.indices) {
    Text(self.businessHours)
}

This works in the console

for businesshour in businessHours {
print("Hours: \(businesshour).")
}

What am I doing wrong here?

3
  • Please add a Minimal, Reproducible Example. Commented Oct 28, 2020 at 18:56
  • I'm not going to be able to do this without providing my API key Commented Oct 28, 2020 at 19:05
  • You may use the JSON response you receive from the server. No need for the API key. Commented Oct 28, 2020 at 19:06

3 Answers 3

3

There is a much simpler way..

When you have an array where the values are separated by ", " you can divide the array with this comma symbol

For Example: I have a user model with food. Food is an array, which looks similar like this:

user.food = ["Banana", "Strawberry", "Apple"]

You noticed: Every item is separated by the comma.

Now you can simply use

Text(user.food.joined(separator: ", "))
   .multilineTextAlignment(.trailing)
   .padding()

Et voilá :)

Sign up to request clarification or add additional context in comments.

Comments

1

Here is a demo that works. Tested with Xcode 12 / iOS 14

enter image description here

struct DemoView: View {
    let businessHours = [
         "Monday: 7:00 AM – 7:00 PM",
         "Tuesday: 7:00 AM – 7:00 PM",
         "Wednesday: 7:00 AM – 7:00 PM",
         "Thursday: 7:00 AM – 7:00 PM",
         "Friday: 7:00 AM – 7:00 PM",
         "Saturday: 7:00 AM – 7:00 PM",
         "Sunday: Closed"
     ]

    var body: some View {
        VStack {
            ForEach(businessHours.indices) {
                 Text(self.businessHours[$0])
            }
        }
    }
}

4 Comments

Oh this is interesting. I did some more testing and that does work, but if the data is dumped into a State such as ` @State var businessHours = []` from an API call I get my original behavior. When I print businessHours I get the following in the console ["Monday: 7:00 AM – 7:00 PM", "Tuesday: 7:00 AM – 7:00 PM", "Wednesday: 7:00 AM – 7:00 PM", "Thursday: 7:00 AM – 7:00 PM", "Friday: 7:00 AM – 7:00 PM", "Saturday: 7:00 AM – 7:00 PM", "Sunday: Closed"]
Well... then I assume the issue is in the code you assign values from API to state.
Sorry that isn't making sense since when I print businessHours to the console all the data is present, but it's only taking the first index.
I also found that if I use Text("\(businessHours)" as String) the data is dumped into text
0

I figured it out.

(0..<businessHours.count, id: \.self) { index in
       Text(self.businessHours[index])
}

Comments

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.