簡體   English   中英

類型“ContentView”不符合協議“View”(Xcode - Swift UI)

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

我嘗試在 ContentView(Swift UI)中為我的應用程序實現,Face ID“function,但之后我收到此錯誤 - “類型'ContentView'不符合協議'View'”。當我嘗試修復錯誤時提供解決方案: typealias Body = <#type#>但我不知道該放什么。也許我只是以錯誤的方式實現了Face ID,所以這里是結果代碼源和實現之前的代碼。

后:

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
        }
    }
}

前:

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

首先,select 所有代碼,然后按cmd+i進行代碼縮進。 在代碼中,您在代碼末尾關閉了 3 個大括號,這是不正確的。 添加這 3 個大括號以驗證 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM