You can’t break a String across multiple lines like that if you’ve used single ” to mark it. You need to use either a multiline String delimited with triple quotes ””” or make your String single line and replace the line breaks with \n.
When I say exactly, I mean copy the above and past it into your project exactly as it is. Don’t try reformatting the line so that the data items line up under each other in the editor window because that won’t work. You cannot split up a string across multiple lines as Roosterboy pointed out.
Are you getting any error messages in the Debug Console?
Yes, the code is exactly the same, except that I replaced
ForEach(todo, id: \.self)
with:
ForEach(todo)
Otherwise I get the following error: “Type ‘()’ cannot conform to ‘View’; only struct/enum/class types can conform to protocols”.
I didn’t split up that line in the Text field, it’s exactly as you quoted.
I’m not getting any error messages in the Console, just a blank screen, both in the console and the simulator (except for the navigation bar that reads “SwiftUI Example”).
On another note, thank you for sending your screenshot. The result is very similar to what you would get if you used the List method, but my question was actually about how to display one single element of the Array, that’s why I wanted to avoid using List. I guess my questions were kind of confusing. Sorry about that, I’m not yet familiar with the coding language (plus, English is not my native language).
When you download Xcode it requires more disk space than the size of the download file. It will decompress (expand) the file and use up a lot of temporary file space on your hard drive as it goes through the installation process. The Mac Operating System is also using disk space as temporary memory “swap in swap out” space too so yes, despite it saying you have 35Gb, that seems like it is not enough. Also it’s a good idea to shut down every other program so that the only thing running is the update.
To be honest I would not be going to the extent of installing Xcode 12 unless you absolutely need to because, as far as I can tell, there is nothing in my ContentView.swift that requires Xcode 12. If you plan on eventually submitting Apps to the App Store then you will need the latest Xcode.
If you create a new project in Xcode 11.7 and then copy the contents of my ContentView (from the earlier post) and paste it in place of the ContentView code in your new project, I thought it should work.
So it looks like ScrollView can’t be initialized with an empty array, at least not in Xcode 11.7.
If I alter the body property like so:
var body: some View {
NavigationView {
Group {
if !todo.isEmpty {
ScrollView {
VStack(spacing: 10) {
ForEach(todo) { item in
HStack {
Text("userID: \(item.userID)\nID: \(item.id)\nTitle: \(item.title)\nCompleted: \(item.completed ? "True" : "False")")
Spacer()
}
Divider()
}
}
.padding(.horizontal)
}
}
else {
Text("")
//I tried to put EmptyView() here but it just kept crashing
}
}
.navigationBarTitle("SwiftUI Example")
.onAppear() {
self.populateArray()
}
}
}
then it works as expected. You do get a flash of the View from the else clause, but that could be worked around with an empty Text (as I did) or maybe a Color or a loading message or whatever you want.