I want to reduce the lines of code by using extension. But I do not know what happened with the code

Viewed 23

Here is my content view

import SwiftUI

struct MainView: View {
    var body: some View {
        TabView{
            ContentView()
                .tabItem {
                    Image(systemName: "square.grid.2x2")
                    Text("Browse")
                        .font(.footnote)
                }
            
        }//:Tab
    }//: BODY
}//: MainView

struct MainView_Previews: PreviewProvider {
    static var previews: some View {
        MainView()
    }
}

Here is my extension file

extension View {
    func tab(imageName: String, content: String) -> Any{
        return self.tabItem {
                Image(systemName: imageName)
                Text(content)
                .font(.footnote)
            }
    }
}

Then I apply the extension, thus the code would be like this

import SwiftUI

struct MainView: View {
    var body: some View {
        TabView{
            ContentView()
                .tab(imageName: "square.grid.2x2", content: "Browse")
            
        }//:Tab
    }//: BODY
}//: MainView

struct MainView_Previews: PreviewProvider {
    static var previews: some View {
        MainView()
    }
}

And then the error happened

--> Protocol 'Any' as a type cannot conform to 'View'

0 Answers
Related