Skip to main content
Cartoon drawing of a man with a grey beard and glasses.

coofdy.com

Martin Kenny's website

SwiftUI errors with any View vs some View

When writing SwiftUI code, Xcode sometimes complains with an error much like this:

Generic struct 'List' requires that 'some AccessibilityRotorContent' conform to 'View'
Initializer 'init(content:)' requires that 'some AccessibilityRotorContent' conform to 'View'
Struct 'ViewBuilder' requires that 'some AccessibilityRotorContent' conform to 'View'

When this happens, it turns out that I've just written something like this:

struct ContentView: View {
    var body: some View {
        List {
            ForEach(0..<10, id: \.self) { _ in
                MyView()
                    .myNewModifier()
            }
        }
    }
}

struct MyView: View {
    var body: some View {
        Text("MyView")
    }
    
    func myNewModifier() -> any View {
        self.foregroundColor(.red)
    }
}

The subtle error is that the signature of myNewModifier should have looked like this:

func myNewModifier() -> some View

Note the some vs the any in my non-compiling code.

Postscript

I was going to put this down to inattention on my part, but as I was writing this post, I noticed what the sequence of events is:

  1. I write a function signature like

    func myNewModifier() -> View
  2. Xcode responds with the following error and Fix-it:

    Use of protocol 'View' as a type must be written 'any View'
    Replace 'View' with 'any View'  [Fix]
    
  3. I click Fix, and the scene is set for me to run around like a headless chook, looking for the mystery error.