Xcode &引用;类型';()';不能符合';视图';;只有结构/枚举/类类型才能符合协议;

Xcode &引用;类型';()';不能符合';视图';;只有结构/枚举/类类型才能符合协议;,xcode,authentication,swiftui,geometryreader,Xcode,Authentication,Swiftui,Geometryreader,编辑:问题解释 此代码产生错误,海报不知道如何解决。 这是前一篇文章的新编辑 我在>几何体读取器上收到错误。这篇文章包含了所有的代码。这篇新文章包含了要求的注册和登录代码。我希望它是可读的格式。我做了一些更正,希望能有所帮助。代码如下所示: import SwiftUI struct ContentView: View { var body: some View { Home() // for light sta

编辑:问题解释

此代码产生错误,海报不知道如何解决。

这是前一篇文章的新编辑

我在>几何体读取器上收到错误。这篇文章包含了所有的代码。这篇新文章包含了要求的注册和登录代码。我希望它是可读的格式。我做了一些更正,希望能有所帮助。代码如下所示:

import SwiftUI

struct ContentView: View {

    var body: some View {
       
            Home()
                // for light status bar...
            .preferredColorScheme(.dark)
            
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
    struct Home : View {
        
        @State var index = 0
    
        var body: some View{
            
            GeometryReader{ _ in // ( ERROR MESSAGE OCCURS HERE)--> ("Type '()' cannot conform to'View'; only struct/enum/class types can conform to protocols" and  "Required by generic struct 'GeometryReader' where 'Content' = '()'")
            
                VStack{
                
                    Image("logo")
                    .resizable()
                    .frame(width:60, height: 60)
                      
                    ZStack{
                        
                        SignUp(index: self.$index)
                            *// changing view order...*
                            .zIndex(Double(self.index))
                        
                        Login(index: self.$index)
                        
                        
                    }
                    
                    HStack(spacing: 15){
                        
                        Rectangle()
                        .fill(Color("Blue"))
                        .frame(height: 1)
                        
                        Text("OR")
                        
                        Rectangle()
                        .fill(Color("Blue"))
                        .frame(height: 1)
                    }
                    .padding(.horizontal, 20)
                    .padding(.top, 50)
                    *// because login button is moved 25 in y axis and 25 padding = 50*
                  
                    
.background(Color("Orange").edgesIgnoringSafeArea(.all))
                    
                    
                    //Curve...
                    
                    HStack(spacing: 25){
                    
                        Button(action: {
                            
                        }) {
                                 Image("Unknown")
                                 .resizable()
                                 .renderingMode(.original)
                                 .frame(width: 50, height: 50)
                                 .clipShape(Circle())
                     }
                                 Button(action: {
                                 }) {
                                     
                                     Image("fb")
                                     .resizable()
                                     .renderingMode(.original)
                                     .frame(width: 50, height: 50)
                                     .clipShape(Circle())
                    }
                                     
                                     Button(action: {
                                     }) {
                                         
                                         Image("instagram")
                                         .resizable()
                                         .renderingMode(.original)
                                         .frame(width: 50, height: 50)
                                         .clipShape(Circle())
                                    
                            }
}
                    .padding(.top, 30)
                }
                .padding(.vertical)
                


struct CShape: Shape {
    func path(in rect: CGRect) -> Path {
        
        return Path {path in
            
            *//right side curve...*
            
            path.move(to: CGPoint(x: rect.width, y: 100))
            path.addLine(to: CGPoint(x: rect.width, y: rect.height))
            path.addLine(to: CGPoint(x: 0, y: rect.height))
            path.addLine(to: CGPoint(x: 0, y: 0))
        }
    
}
}
        
        
    struct CShape1: Shape {
        func path(in rect: CGRect) -> Path {
            
            return Path {path in
                
                *//left side curve...*
                
                path.move(to: CGPoint(x: 0, y: 100))
                path.addLine(to: CGPoint(x: 0, y: rect.height))
                path.addLine(to: CGPoint(x: rect.width, y: rect.height))
                path.addLine(to: CGPoint(x: rect.width, y: 0))
            }
        
    }
    }
                
struct Login : View {
            
            @State var email = ""
            @State var pass = ""
            @Binding var index : Int
            
            var body : some View {
                
                ZStack(alignment: .bottom) {
                    
                    VStack{
                        
                        HStack{
                            
                            VStack(spacing:10){
                                
                                Text("Login")
                                    .foregroundColor(self.index == 0 ? .white : .gray)
                                    .font(.title)
                                    .fontWeight(.bold)
                                
                                Capsule()
                                    .fill(self.index == 0 ? Color.blue : Color.clear)
                                    .frame(width:100, height: 5)
                            }
                            
                            Spacer(minLength:0)
                        }
                        .padding(.top, 30)// for top curve...
                        VStack{
                            
                            HStack(spacing:15){
                                
                                Image(systemName: "envelope")
                                .foregroundColor(Color("Blue"))
                           
                                TextField("Email Adress", text: self.$email)
                            }
                            Divider().background(Color.white.opacity(0.5))
                        }
                        .padding(.horizontal)
                        .padding(.top, 40)
                    
                    VStack{
                        
                        HStack(spacing:15){
                            
                            Image(systemName: "eye")
                            .foregroundColor(Color("Orange"))
                       
                            SecureField("Password", text: self.$pass)
                        }
                        Divider().background(Color.white.opacity(0.5))
                    }
                    .padding(.horizontal)
                    .padding(.top, 30)
                        
                        HStack{
                            
                            Spacer(minLength: 0)
                            
                            Button(action: {
                                
                            }) {
                                
                                Text("Forget Password?")
                                    .foregroundColor(Color.white.opacity(0.6))
                            }
                        }
                        .padding(.horizontal)
                        .padding(.top, 30)
                    }
                    .padding()
                    // bottom padding...
                    .padding(.bottom, 65)
                    .background(Color("LightBlue"))
                    .clipShape(CShape())
                    .contentShape(CShape())
                    .shadow(color: Color.black.opacity(0.3), radius: 5, x: 0, y: -5)
                    .onTapGesture{
                            
                        self.index = 0
                            
                    }
                    .cornerRadius(35)
                    .padding(.horizontal,20)
                    
                    // Button...
                    
                    Button(action: {
                        
                    }) {
                        
                        Text("LOGIN")
                            .foregroundColor(.white)
                            .fontWeight(.bold)
                            .padding(.vertical)
                            .padding(.horizontal, 50)
                        .background(Color("LightBlue"))
                        .clipShape(Capsule())
                        // shadow ...
                            .shadow(color: Color.white.opacity(0.1), radius: 5, x: 0, y: 5)
                    }
                    // moving view down...
                        .offset(y: 25)
                        .opacity(self.index == 0 ? 1 : 0)
                }
            }
        }
                
// SignUp Page...
        
struct SignUp : View {
                        
    @State var email = ""
    @State var pass = ""
    @State var Repass = ""
    @Binding var index: Int
                        
        var body : some View {
            
            ZStack(alignment: .bottom) {
                
                VStack{
                    
                    HStack{
                        
                        Spacer(minLength:0)
                        
                        VStack(spacing: 10){
                        
                            Text("SignUp")
                                .foregroundColor(self.index == 1 ? .white : .gray)
                                .font(.title)
                                .fontWeight(.bold)
                        
                            Capsule()
                                .fill(self.index == 1 ? Color.blue : Color.clear)
                                .frame(width:100, height: 5)
                        }
                    }
                    .padding(.top, 30)// for top curve...
                    VStack{
                        
                        HStack(spacing:15){
                            
                            Image(systemName: "envelope")
                            .foregroundColor(Color("Orange"))
                       
                            TextField("Email Adress", text: self.$email)
                        }
                        Divider().background(Color.white.opacity(0.5))
                    }
                    .padding(.horizontal)
                    .padding(.top, 40)
                
                    VStack{
                    
                    HStack(spacing:15){
                        
                        Image(systemName: "eye")
                        .foregroundColor(Color("Orange"))
                   
                        SecureField("Password", text: self.$pass)
                    }
                    Divider().background(Color.white.opacity(0.5))
                }
                    .padding(.horizontal)
                    .padding(.top, 30)
                    
                    // replacing forget password with reenter password...
                    // so same height will be maintained...
                    
                    VStack{
                         
                         HStack(spacing:15){
                             
                             Image(systemName: "eye")
                            .foregroundColor(Color("Orange"))
                        
                             SecureField("Password", text: self.$Repass)
                         }
                         Divider().background(Color.white.opacity(0.5))
                     }
                         .padding(.horizontal)
                         .padding(.top, 30)
                }
                .padding()
                // bottom padding...
                .padding(.bottom, 65)
                .background(Color("Blue"))
                .clipShape(CShape1())
                //clipping the content shape also for tap gesture...
                .contentShape(CShape1())
                // shadow...
                .shadow(color: Color.black.opacity(0.3), radius: 5, x: 0, y: -5)
                .onTapGesture {
                    
                    self.index = 1
                    
                }
                .cornerRadius(35)
                .padding(.horizontal,20)
                
                *// Button...*
                
                Button(action: {
                    
                }) {
                    
                  Text("SIGNUP")
                    .foregroundColor(.white)
                    .fontWeight(.bold)
                    .padding(.vertical)
                    .padding(.horizontal, 50)
                    .background(Color("Blue"))
                    .clipShape(Capsule())
                    *// shadow ...*
                    .shadow(color: Color.white.opacity(0.1), radius: 5, x: 0, y: 5)
                }
                *// moving view down...*
                    .offset(y: 25)
            *// hiding view when its in background...*
            *// only button...*
                    .opacity(self.index == 1 ? 1 : 0)

        }
    }
}

}
}

}

        
            

因此,代码中的问题是,您在
geometryreader
中定义视图,这是一个很大的禁忌。因此,解决方法是将
登录名
单选
移动到
geometryreader
之外,或者更好、更好的做法是为每个视图创建一个新文件,并将其代码添加到该文件中。例如,一个文件用于
Login.swift
,另一个文件用于
Register.swift
,可能还有另一个名为
Shapes
的文件,其中包括多个形状并将其导出

你所做的与此类似

struct ContentView: View {
    var body: some View {
          GeomtryReader { _ in
                Text("test")

                // Here is where the bug would happen
                struct NewView: View {
                      var body: some View {
                            Text("Second View")
                      } 
                }
                //////////////////////////////////////
          }
    }
}
您可以看到,如果复制并粘贴上述代码,它将生成相同的错误。您应该做的是将
NewView
移动到
geometryreader

像这样的

struct ContentView: View {
    var body: some View {
          return GeomtryReader { _ in
                Text("test")
          }
          
          // This will fix the error
          struct NewView: View {
                var body: some View {
                      Text("Second View")
                } 
          }
          //////////////////////////////////////
    }
}
注意我把代码移到了哪里。还请注意,我已将
Return
添加到
geometryreader
中,这是因为
body
是一个计算属性,它的值应为
View
,但在这种情况下,我们会混淆编译器,因为我们希望它是哪个
视图
的返回值,因此必须手动指定它。如果您不想包含
return
,那么您必须将
NewView
移到
body
之外,或者更好地移到
ContentView
之外

在任何情况下,这里是你的代码工作100%,你可以复制和粘贴它

import SwiftUI

struct ContentView: View {
    
    var body: some View {
        
        Home()
            // for light status bar...
            .preferredColorScheme(.dark)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct Home : View {
    
    @State var index = 0
    
    var body: some View{
        
        return GeometryReader{ _ in // ( ERROR MESSAGE OCCURS HERE)--> ("Type '()' cannot conform to'View'; only struct/enum/class types can conform to protocols" and  "Required by generic struct 'GeometryReader' where 'Content' = '()'")
            
            VStack{
                
                Image("logo")
                    .resizable()
                    .frame(width:60, height: 60)
                
                ZStack{
                    
                    SignUp(index: self.$index)
                        // changing view order...*
                        .zIndex(Double(self.index))
                    
                    Login(index: self.$index)
                    
                    
                }
                
                HStack(spacing: 15){
                    
                    Rectangle()
                        .fill(Color("Blue"))
                        .frame(height: 1)
                    
                    Text("OR")
                    
                    Rectangle()
                        .fill(Color("Blue"))
                        .frame(height: 1)
                }
                .padding(.horizontal, 20)
                .padding(.top, 50)
                    // because login button is moved 25 in y axis and 25 padding = 50*
                    
                    
                    .background(Color("Orange").edgesIgnoringSafeArea(.all))
                
                
//                Curve...
                
                HStack(spacing: 25){
                    
                    Button(action: {
                        
                    }) {
                        Image("Unknown")
                            .resizable()
                            .renderingMode(.original)
                            .frame(width: 50, height: 50)
                            .clipShape(Circle())
                    }
                    Button(action: {
                    }) {
                        
                        Image("fb")
                            .resizable()
                            .renderingMode(.original)
                            .frame(width: 50, height: 50)
                            .clipShape(Circle())
                    }
                    
                    Button(action: {
                    }) {
                        
                        Image("instagram")
                            .resizable()
                            .renderingMode(.original)
                            .frame(width: 50, height: 50)
                            .clipShape(Circle())
                        
                    }
                }
                .padding(.top, 30)
            }
            .padding(.vertical)
            
            
            
            
            
            
            
            
            
        }
        
        struct Login : View {
            
            @State var email = ""
            @State var pass = ""
            @Binding var index : Int
            
            var body : some View {
                
                ZStack(alignment: .bottom) {
                    
                    VStack{
                        
                        HStack{
                            
                            VStack(spacing:10){
                                
                                Text("Login")
                                    .foregroundColor(self.index == 0 ? .white : .gray)
                                    .font(.title)
                                    .fontWeight(.bold)
                                
                                Capsule()
                                    .fill(self.index == 0 ? Color.blue : Color.clear)
                                    .frame(width:100, height: 5)
                            }
                            
                            Spacer(minLength:0)
                        }
                            .padding(.top, 30)// for top curve...
                        VStack{
                            
                            HStack(spacing:15){
                                
                                Image(systemName: "envelope")
                                    .foregroundColor(Color("Blue"))
                                
                                TextField("Email Adress", text: self.$email)
                            }
                            Divider().background(Color.white.opacity(0.5))
                        }
                        .padding(.horizontal)
                        .padding(.top, 40)
                        
                        VStack{
                            
                            HStack(spacing:15){
                                
                                Image(systemName: "eye")
                                    .foregroundColor(Color("Orange"))
                                
                                SecureField("Password", text: self.$pass)
                            }
                            Divider().background(Color.white.opacity(0.5))
                        }
                        .padding(.horizontal)
                        .padding(.top, 30)
                        
                        HStack{
                            
                            Spacer(minLength: 0)
                            
                            Button(action: {
                                
                            }) {
                                
                                Text("Forget Password?")
                                    .foregroundColor(Color.white.opacity(0.6))
                            }
                        }
                        .padding(.horizontal)
                        .padding(.top, 30)
                    }
                    .padding()
                        // bottom padding...
                        .padding(.bottom, 65)
                        .background(Color("LightBlue"))
                        .clipShape(CShape())
                        .contentShape(CShape())
                        .shadow(color: Color.black.opacity(0.3), radius: 5, x: 0, y: -5)
                        .onTapGesture{
                            
                            self.index = 0
                            
                    }
                    .cornerRadius(35)
                    .padding(.horizontal,20)
                    
                    // Button...
                    
                    Button(action: {
                        
                    }) {
                        
                        Text("LOGIN")
                            .foregroundColor(.white)
                            .fontWeight(.bold)
                            .padding(.vertical)
                            .padding(.horizontal, 50)
                            .background(Color("LightBlue"))
                            .clipShape(Capsule())
                            // shadow ...
                            .shadow(color: Color.white.opacity(0.1), radius: 5, x: 0, y: 5)
                    }
                        // moving view down...
                        .offset(y: 25)
                        .opacity(self.index == 0 ? 1 : 0)
                }
            }
        }
        
        //SignUp Page...

        struct SignUp : View {
            
            @State var email = ""
            @State var pass = ""
            @State var Repass = ""
            @Binding var index: Int
            
            var body : some View {
                
                ZStack(alignment: .bottom) {
                    
                    VStack{
                        
                        HStack{
                            
                            Spacer(minLength:0)
                            
                            VStack(spacing: 10){
                                
                                Text("SignUp")
                                    .foregroundColor(self.index == 1 ? .white : .gray)
                                    .font(.title)
                                    .fontWeight(.bold)
                                
                                Capsule()
                                    .fill(self.index == 1 ? Color.blue : Color.clear)
                                    .frame(width:100, height: 5)
                            }
                        }
                            .padding(.top, 30)// for top curve...
                        VStack{
                            
                            HStack(spacing:15){
                                
                                Image(systemName: "envelope")
                                    .foregroundColor(Color("Orange"))
                                
                                TextField("Email Adress", text: self.$email)
                            }
                            Divider().background(Color.white.opacity(0.5))
                        }
                        .padding(.horizontal)
                        .padding(.top, 40)
                        
                        VStack{
                            
                            HStack(spacing:15){
                                
                                Image(systemName: "eye")
                                    .foregroundColor(Color("Orange"))
                                
                                SecureField("Password", text: self.$pass)
                            }
                            Divider().background(Color.white.opacity(0.5))
                        }
                        .padding(.horizontal)
                        .padding(.top, 30)
                        
                        // replacing forget password with reenter password...
                        // so same height will be maintained...
                        
                        VStack{
                            
                            HStack(spacing:15){
                                
                                Image(systemName: "eye")
                                    .foregroundColor(Color("Orange"))
                                
                                SecureField("Password", text: self.$Repass)
                            }
                            Divider().background(Color.white.opacity(0.5))
                        }
                        .padding(.horizontal)
                        .padding(.top, 30)
                    }
                    .padding()
                        // bottom padding...
                        .padding(.bottom, 65)
                        .background(Color("Blue"))
                        .clipShape(CShape1())
                        //clipping the content shape also for tap gesture...
                        .contentShape(CShape1())
                        // shadow...
                        .shadow(color: Color.black.opacity(0.3), radius: 5, x: 0, y: -5)
                        .onTapGesture {
                            
                            self.index = 1
                            
                    }
                    .cornerRadius(35)
                    .padding(.horizontal,20)
                    
                    // Button...*
                    
                    Button(action: {
                        
                    }) {
                        
                        Text("SIGNUP")
                            .foregroundColor(.white)
                            .fontWeight(.bold)
                            .padding(.vertical)
                            .padding(.horizontal, 50)
                            .background(Color("Blue"))
                            .clipShape(Capsule())
                            // shadow ...*
                            .shadow(color: Color.white.opacity(0.1), radius: 5, x: 0, y: 5)
                    }
                        // moving view down...*
                        .offset(y: 25)
                        // hiding view when its in background...*
                        // only button...*
                        .opacity(self.index == 1 ? 1 : 0)
                    
                }
            }
        }
        
        struct CShape: Shape {
            func path(in rect: CGRect) -> Path {
                
                return Path {path in
                    
                    //right side curve...*
                    
                    path.move(to: CGPoint(x: rect.width, y: 100))
                    path.addLine(to: CGPoint(x: rect.width, y: rect.height))
                    path.addLine(to: CGPoint(x: 0, y: rect.height))
                    path.addLine(to: CGPoint(x: 0, y: 0))
                }
                
            }
        }

        struct CShape1: Shape {
            func path(in rect: CGRect) -> Path {
                
                return Path {path in
                    
                    //left side curve...*
                    
                    path.move(to: CGPoint(x: 0, y: 100))
                    path.addLine(to: CGPoint(x: 0, y: rect.height))
                    path.addLine(to: CGPoint(x: rect.width, y: rect.height))
                    path.addLine(to: CGPoint(x: rect.width, y: 0))
                }
                
            }
        }
    }
    


}

你能修好你的代码让我们运行吗?你贴的东西到处都是。不过,您的问题似乎很容易解决。如果我使用您的代码并修复格式,它似乎可以工作,因此您发布的内容(换句话说)是不可复制的。很抱歉,很难阅读。我接受了你的更正,我希望这样更好。谢谢,穆汉德!你好,悉尼,欢迎来到SO!我已经测试了你的代码,但是我不得不用文本组件替换
注册
登录
,因为你没有在问题中包含它们的代码。测试为我运行,没有任何错误。您是否可以发布
SignUp()
Login()
查看代码?如果是这样的话,我很高兴明天下班后给你回电话。@Sydney一点也不担心。我同意塞缪尔·伊赫所说的。我也做了同样的事。我用Text()替换了注册和登录,效果非常好。我怀疑你的问题在其他视图中,如果你一次评论一个,看看是哪一个产生了错误,然后在这里发布它,这就是视图的代码,那将是非常有帮助的。非常感谢你的帮助和详细的解释。我对编码非常陌生,您的回答使我对错误的理解更加清晰。我真的很感谢你的帮助,也感谢你花时间来帮助我。@Sydney很高兴我能帮上忙!别犹豫再问一次!我们是来帮忙的。另外,请考虑将答案标记正确,以便其他人更容易找到答案。