类型“ContentView”不符合协议“View”(Xcode - Swift UI)

Posted

技术标签:

【中文标题】类型“ContentView”不符合协议“View”(Xcode - Swift UI)【英文标题】:Type 'ContentView' does not conform to protocol 'View' (Xcode - Swift UI) 【发布时间】:2021-01-05 13:40:48 【问题描述】:

我尝试在 ContentView(Swift UI) 中为我的应用程序实现 ,,Face ID" 功能,但之后我收到此错误 - "Type 'ContentView' does not conform to protocol '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

【问题讨论】:

【参考方案1】:

首先,选择所有代码并按cmd+i 进行代码缩进。 在代码中,您在代码末尾关闭了 3 个大括号,这是不正确的。添加这 3 个大括号用于身份验证功能。

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

【讨论】:

谢谢伙计!现在按我的意愿工作

以上是关于类型“ContentView”不符合协议“View”(Xcode - Swift UI)的主要内容,如果未能解决你的问题,请参考以下文章

请帮忙:类型'()'不能符合'View';只有结构/枚举/类类型可以符合协议

使用 if 语句时:类型 '()' 不能符合 'View';只有结构/枚举/类类型可以符合协议

类型 '()' 不能符合 'View';只有 struct/enum/class 类型可以符合使用 swift ui 调用调用函数的协议

type() 不能符合 View;只有结构/枚举/类类型可以符合协议

“类型‘()’不能符合‘视图’;只有结构/枚举/类类型可以符合协议”

Scroll View 内的 ContentView 不增加高度