当我尝试运行我的代码时,我在我的应用程序文件中有一个错误,因为在ContentView中缺少参数的参数。因此,我通过为参数输入参数来解决这个问题。
这是我所做的(这解决了我之前的错误):
@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)
})
}
}
}
当我做这些更改时,它解决了之前的错误。但是现在我得到了错误:
类型'Market_ExampleApp'不符合协议'App'
最初我认为init是app协议所必需的,并尝试了这个方法,但它不起作用。
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()
}
}
}
}
主页代码:
@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
}
裤子结构:
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(当衬衫被点击时,它应该带用户到一个详细的视图)。MarketplaceTrouserView的代码是相同的
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)
}
}
}
调用ShirtDetailView时发生错误。错误如下:
无法将类型为"Binding"的值"$showDetailShirt"转换为预期类型为"Bool"的值,请使用包装值代替
无法将"Binding"类型的值转换为期望的参数类型"Shirt">
App()
是开始运行一切的主要组件。
App
中不能有@Binding
变量,否则会导致错误
App不符合协议App