swiftxcodeswiftuiswift-protocolscontentview

Type 'ContentView' does not conform to protocol 'View' (Xcode - Swift UI)


I tried to implement ,,Face ID" function to my app in ContentView(Swift UI) but after that I get this error - "Type 'ContentView' does not conform to protocol 'View'". When I tried to fix the error it offers solution: typealias Body = <#type#> but I don’t know what to put there. Maybe I just I implemented the Face ID in the wrong way, so here’s the result code source and the one before implementation.

After:

import SwiftUI
import LocalAuthentication

struct ContentView : View {
    
    
    @State private var isUnlocked = false
    @ObservedObject var service: DataService = .shared
    
    var categories:[String:[Goal]] {
        .init(
            grouping: service.goals,
            by: {$0.category.rawValue}
        )
    }
    
    func authenticate() {
        let context = LAContext()
        var error: NSError?

        // check whether biometric authentication is possible
        if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
            // it's possible, so go ahead and use it
            let reason = "We need to unlock your data."

            context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, authenticationError in
                // authentication has now completed
                DispatchQueue.main.async {
                    if success {
                        self.isUnlocked = true
                    } else {
                        // there was a problem               
                }
                }
    
    var body: some View {
        VStack {
            if self.isUnlocked {
        NavigationView{
            List (categories.keys.sorted(), id:\.self) {key in
                GoalRow(categoryName: "Level \(key)".uppercased(), goals: self.categories[key]!)
                    .frame(height: 320)
                .padding(.top)
                .padding(.bottom)
            }
            .navigationBarTitle(Text("Future"))
        }
            } else {
                Text("Locked")
            }
        }
        .onAppear(perform: authenticate)
  }
}

#if DEBUG
struct Content_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif
        }
    }
}

Before:

import SwiftUI

struct ContentView : View {
    
    @ObservedObject var service: DataService = .shared
    
    var categories:[String:[Goal]] {
        .init(
            grouping: service.goals,
            by: { $0.category.rawValue }
        )
    }
    
    
    var body: some View {
        NavigationView{
            List (categories.keys.sorted(), id:\.self) {key in
                GoalRow(categoryName: "Level \(key)".uppercased(), goals: self.categories[key]!)
                    .frame(height: 320)
                .padding(.top)
                .padding(.bottom)
            }
            .navigationBarTitle(Text("Future"))
        }
    }

}

#if DEBUG
struct Content_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

Solution

  • First, select all code and press cmd+i for code indentation. And in code, you have closed 3 curly brackets at the end of the code which is incorrect. Add these 3 curly brackets for authenticate function.

    struct ContentView : View {    
        @State private var isUnlocked = false
        @ObservedObject var service: DataService = .shared
        
        var categories:[String:[Goal]] {
            .init(
                grouping: service.goals,
                by: {$0.category.rawValue}
            )
        }
        
        func authenticate() {
            let context = LAContext()
            var error: NSError?
            
            // check whether biometric authentication is possible
            if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
                // it's possible, so go ahead and use it
                let reason = "We need to unlock your data."
                
                context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, authenticationError in
                    // authentication has now completed
                    DispatchQueue.main.async {
                        if success {
                            self.isUnlocked = true
                        } else {
                            // there was a problem
                        }
                    }
                }
            }
        }
        
        
        //Your body view
    }
    
    #if DEBUG
    struct Content_Previews : PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    #endif