See sample code
@State private var citiesList: [City]?
var body: some View {
if let cities {
content(cities)
}
else {
AsyncView(
operation: { try await getData(()) },
content: { cities in
contentView(cities)
}
)
}
}
So this works ok. my issue is when adding a code above contentView(cities) since it seems this kind of closure only accepts 1 line, returning a view and does not accept any other code?
like if i add the line
citiesList = cities contentView(cities)
it will say Type '()' cannot conform to 'View'
What is the solution for this?
Or is it possible that the "cities in" part, i can assign it to the citiesList, so that citiesList will be passed to the content(citiesList) method instead of content(cities) ?
it seems this kind of closure only accepts 1 line
That's not the point. But you need to have views only.
The code does not compile at all because it is very incomplete, so very hard to understand what you need precisely.
- You do not tell how cities is defined at top for if let cities
- what is content ? do you mean ContentView ?
- what is AsyncView ? Is it something you imported form here : https://www.ralfebert.com/ios-app-development/swiftui/asyncview/
Several ways to solve it such as:
- add this code in a modifier as .onAppear
if let cities {
contentView(cities)
.onAppear { citiesList = cities }
}
But please explain more the context of this part of code.