Home > OS >  App doesn't conform to protocol 'App' after adding arguments for parameters
App doesn't conform to protocol 'App' after adding arguments for parameters

Time:07-20

When I try to run my code I had an error in my app file for missing arguments for parameters in ContentView. Therefore I fixed this issue by entering arguments for the parameters.

Here is what I did (which resolved my previous error):

@main
struct Market_ExampleApp: App {

    @StateObject var appModel: AppViewModel = .init()
    @StateObject var sharedData: SharedDataModel = SharedDataModel()
    @Binding var shirtData : Shirt
    @Binding var showDetailShirt: Bool
    @Binding var trouserData : Trouser
    @Binding var showDetailTrouser: Bool

    init(shirtData: Binding<Shirt>, showDetailShirt: Binding<Bool>, trouserData: Binding<Trouser>, showDetailTrouser: Binding<Bool>){
        self._shirtData = shirtData
        self._showDetailShirt = showDetailShirt
        self._trouserData = trouserData
        self._showDetailTrouser = showDetailTrouser

        UITabBar.appearance().isHidden = true
    }
    
    // Calling Delegate...
    @UIApplicationDelegateAdaptor(Appdelegate.self) var delegate
    var body: some Scene {
        WindowGroup {
            ContentView(shirtData: $shirtData, showDetailShirt: $showDetailShirt, trouserData: $trouserData, showDetailTrouser: $showDetailTrouser)
                .onOpenURL(perform: { url in
                    
                    Auth.auth().canHandle(url)
                })
        }
    }
}

When I made these changes, it resolved the previous error. However now I get the error:

Type 'Market_ExampleApp' does not conform to protocol 'App'

Originally I thought an init was required for the app protocol and tried this method but it didn't work.

ContentView:

import SwiftUI

struct ContentView: View {
  
    @AppStorage("log_status") var log_Status: Bool = false
    @StateObject var appModel: AppViewModel = .init()
    
    @StateObject var sharedData: SharedDataModel = SharedDataModel()
    
    @Binding var shirtData : Shirt
    @Binding var showDetailShirt: Bool

    @Binding var trouserData : Trouser
    @Binding var showDetailTrouser: Bool
    
    init(shirtData: Binding<Shirt>, showDetailShirt: Binding<Bool>, trouserData: Binding<Trouser>, showDetailTrouser: Binding<Bool>){
        self._shirtData = shirtData
        self._showDetailShirt = showDetailShirt
        self._trouserData = trouserData
        self._showDetailTrouser = showDetailTrouser
        
        UITabBar.appearance().isHidden = true
    }
    
    var body: some View {
        Group{
            if log_Status{
                MainPage(shirtData: $shirtData, showDetailShirt: $showDetailShirt, trouserData: $trouserData, showDetailTrouser: $showDetailTrouser)
            }
            else{
                OnBoardingPage()
            }
        }
    }
}

MainPage code:

    @Binding var shirtData : Shirt
    @Binding var showDetailShirt: Bool

    @Binding var trouserData : Trouser
    @Binding var showDetailTrouser: Bool

    @Namespace var animation
    
    // Hiding Tab Bar...
    init(shirtData: Binding<Shirt>, showDetailShirt: Binding<Bool>, trouserData: Binding<Trouser>, showDetailTrouser: Binding<Bool>){
        self._shirtData = shirtData
        self._showDetailShirt = showDetailShirt
        self._trouserData = trouserData
        self._showDetailTrouser = showDetailTrouser
        
        UITabBar.appearance().isHidden = true
    }

Struct for Shirt:

import SwiftUI
import FirebaseFirestoreSwift
import Firebase

struct Shirt: Identifiable, Codable {
    
    @DocumentID var id: String?
    var shirt_name: String = ""
    var shirt_type: String = ""
    var shirt_image: String = ""
    var shirt_uid : String = ""
    var shirt_stock: Int = 0
}

Struct for Trouser:

import SwiftUI
import FirebaseFirestoreSwift
import Firebase

struct Trouser: Identifiable, Codable {
    
    @DocumentID var id: String?
    var trouser_name: String = ""
    var trouser_type: String = ""
    var trouser_image: String = ""
    var trouser_uid : String = ""
}

MarketplaceShirtView (when a shirt is tapped it should take user to a detailed view). The code for MarketplaceTrouserView is identical

import SwiftUI

struct MarketplaceShirtView: View {
    
    @EnvironmentObject var appModel: AppViewModel
    @StateObject var MarketplaceModel = MarketplaceViewModel()
    @State private var selectedMarketplaceFilter: MarketplaceFilterViewModel = .shirt
    @Namespace var animation : Namespace.ID 
    @State var showDetailShirt = false
    @State var selectedShirt : Shirt!
    
    @EnvironmentObject var sharedData: SharedDataModel
    
    var body: some View {
        
        var columns = Array(repeating: GridItem(.flexible()), count: 2)
        
        ZStack{
            
            VStack(spacing: 10){
                                
                
                HStack {
                    
                    Text("Find Shirts To Buy")
                        
                    }
                
                 }
            }
                
                if MarketplaceModel.shirts.isEmpty{
                    
                    ProgressView()
                   
                }
                else{
                    
                    ScrollView {
                        

                        LazyVGrid(columns: Array(repeating: GridItem(){
                            
                            ForEach(MarketplaceModel.filteredShirt){shirt in
                                                                
                                    ShirtView(shirtData: shirt)

                                        .onTapGesture {
                                            
                                                selectedShirt = shirt
                                                showDetailShirt.toggle()
     
                                        }
                            }
                        }
                    })
                }
            }
            
            if selectedShirt != nil && showDetailShirt{
                
                ShirtDetailView(shirtData: Binding($selectedShirt)!, showDetailShirt: $showDetailShirt,animation: _animation)
            }
}
}

Errors occur when ShirtDetailView is called. The errors are :

Cannot convert value '$showDetailShirt' of type 'Binding' to expected type 'Bool', use wrapped value instead

Cannot convert value of type 'Binding' to expected argument type 'Shirt'

CodePudding user response:

App() is the main component which starts running everything.

You can't have @Binding Variable in App, it will cause an error

App does not conform to protocol App

  • Related