Unexpected platform condition (Expected `os`, `arch` or swift

Hello,

I’m trying to implement the following in a Multiplatform project:

struct ContentView: View {
    var body: some View {
        List {
            Section(header: Text("Main Treats"), footer: Text("More comming soon..")){
                ForEach(0..<5){
                    Text("\($0)")
                        .tag($0)
                }
            }
            #if os(iOS)
                .listStyle(GroupedListStyle())
            #endif
        }
    }
}

But I’m getting this error:

Screen Shot 2020-09-24 at 4.05.25 PM

Any of you knows how can fix this error?

Hi,

It looks like you cannot embed modifiers inside compiler conditionals but you can make a view modifier which uses the conditional.

struct MultiplatformListModifier: ViewModifier {
  func body(content: Content) -> some View {
    #if os(iOS)
    return content.listStyle(GroupedListStyle())
    #endif
    return content
  }
}

You can use the modifier like this:

List {
     // ...
}
 .modifier(MultiplatformListModifier())

Hope this helps.

Hi @luisme, were you able to try out @sarah’s recommendation? Please let us know if you are still having an issue.

Best,
Gina

Hello @gdelarosa, Yes and works. Thank you

1 Like