Function declares an opaque return type 'some View', but the return statements in its body do not have matching underlying types
Asked Answered
M

2

7

I am getting the error where var view: some View{.

enum HomeButtons: Int, Hashable, CaseIterable{
    case registerSignal = 1
    case setAlarm = 2
    case tV = 3
    case test = 4
    
    var image: String{
        switch self{
        case .registerSignal:
            return "wave.3.backward"
        case .setAlarm:
            return "alarm.fill"
        case .tV:
            return "tv.fill"
        case .test:
            return "av.remote.fill"
        }
    }
    var text: String{
        switch self{
        case .registerSignal:
            return "Register Signal for TV"
        case .setAlarm:
            return "Set up Alarm"
        case .tV:
            return "TV and Sequences"
        case .test:
            return "Test Device"
        }
    }
    var view: some View{                                 <------- Error is displayed here    
        switch self{
        case .registerSignal:
            return RegisterView(title: self.text)
        case .setAlarm:
            return Text("Set up Alarm")
        case .tV:
            return Text("TV and Sequences")
        case .test:
            return Text("Test Device")
        }
    }
}
struct RegisterView: View{
    var title: String
    var body: some View{
        ScrollView{
            ForEach(getTVList(), id: \.TVID){ TV in
                NavigationLink(value: TV.TVID){
                    Text(TV.name)
                }
            }
        }
        .background(Color("ToledoGolden"))
        .foregroundColor(.accentColor)
        .navigationTitle(title)
    }
}

I tried changing var view: some View to var view: any View the error went away but where I was calling HomeButtons.view I got this error message: Type 'any View' cannot conform to 'View'

Modestine answered 28/11, 2022 at 19:6 Comment(0)
H
19

You have to make the variable an @ViewBuilder and remove the return

enum HomeButtons: Int, Hashable, CaseIterable{
    case registerSignal = 1
    case setAlarm = 2
    case tV = 3
    case test = 4
    
    var image: String{
        switch self{
        case .registerSignal:
            return "wave.3.backward"
        case .setAlarm:
            return "alarm.fill"
        case .tV:
            return "tv.fill"
        case .test:
            return "av.remote.fill"
        }
    }
    var text: String{
        switch self{
        case .registerSignal:
            return "Register Signal for TV"
        case .setAlarm:
            return "Set up Alarm"
        case .tV:
            return "TV and Sequences"
        case .test:
            return "Test Device"
        }
    }
    @ViewBuilder var view: some View{
        switch self{
        case .registerSignal:
            RegisterView(title: self.text)
        case .setAlarm:
            Text("Set up Alarm")
        case .tV:
            Text("TV and Sequences")
        case .test:
            Text("Test Device")
        }
    }
}
Haar answered 28/11, 2022 at 21:23 Comment(2)
But, how on earth one can judge this from such a cryptic error message?Ultrasonics
If we are to watch hours of video content, just to be able to decrypt a not-so-helpful compiler error message, I think there is something wrong going on here :) I just hope Apple improves the situation.Ultrasonics
T
0

You could also leave that like you've tried before with "any View" instead of "some View" and in case you need the view just use AnyView(HomeButton.view) instead of a direct call

var view: any View{
get{    //don't forget
    switch self{
    case .registerSignal:
        return RegisterView(title: self.text)
    case .setAlarm: //...

and for usage

AnyView(HomeButton.view)
Tergal answered 29/1, 2024 at 21:56 Comment(1)
AnyView is highly discouraged. You can watch "Demystify SwiftUI"Haar

© 2022 - 2025 — McMap. All rights reserved.