简体   繁体   English

访问 @Published 变量不会编译; “不能符合‘视图’; 只有结构/枚举/类类型可以符合协议”

[英]Access a @Published variable does not compile; “cannot conform to 'View'; only struct/enum/class types can conform to protocols”

Accessing settings.score in Text(...) or changing it in Button() works fine, but when I try to write the value into a variable, the compiler complains... Any help would be great, thank you!在 Text(...) 中访问 settings.score 或在 Button() 中更改它可以正常工作,但是当我尝试将值写入变量时,编译器会抱怨...任何帮助都会很棒,谢谢!

import SwiftUI

struct ContentView: View
{
  @EnvironmentObject var settings: UserSettings // settings are initialized in AppDelegate
  @State var myscore: Int = 0

  var body: some View
  {
    NavigationView
    {
      VStack
      {
        myscore = settings.score // COMPILER FAIL: Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols
        Text("\(settings.score)")   // here score shows up correct!
          .padding()

        Button("Increase Score")
        {
          settings.score += 1 // this also works
        }
        .padding()

        NavigationLink(destination: DetailView())
        {
          Text("Show Detail View")
        }
      }
    }
    .environmentObject(self.settings)}
}
  
 class UserSettings: ObservableObject
{
  @Published var score: Int = 0
  
  init(_ value: Int)
  {
    self.score = value
  }
}

You can not assign value to inside the VStack .您不能将值分配给VStack内部。 You should use .onAppear() .您应该使用.onAppear() And you are using wrong way to set environmentObject而且您使用错误的方式设置environmentObject

Set your environmentObject where you can initialize your ContentView.设置您可以在其中初始化 ContentView 的environmentObject like this像这样

@main
struct ContentViewApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView().environmentObject(UserSettings(15))
        }
    }
}
struct ContentView: View {
    @EnvironmentObject var settings: UserSettings // settings are initialized in AppDelegate
    @State var myscore: Int = 0

    var body: some View {
        NavigationView {
            VStack {
                Text("\(settings.score)")   // here score shows up correct!
                    .padding()
                
                Button("Increase Score") {
                    settings.score += 1 // this also works
                }
                .padding()
                
                NavigationLink(destination: DetailView()) {
                    Text("Show Detail View")
                }
            }
        }
        .onAppear {
            myscore = settings.score //<=== Here
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 navigationBarItems“类型[视图]不能符合'视图'; 只有结构/枚举/类类型可以符合协议” - navigationBarItems “Type [view] cannot conform to 'View'; only struct/enum/class types can conform to protocols” 使用 if 语句时:类型 '()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - While using an if statement: Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 类型 &#39;()&#39; 不能符合 &#39;View&#39;; 只有 struct/enum/class 类型才能符合协议; 使用 SwiftUI 调用函数 - Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols; calling functions with SwiftUI 类型 &#39;() -&gt; ()&#39; 不能符合 &#39;View&#39;; 只有结构/枚举/类类型可以符合协议 - Type '() -> ()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 类型 '()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 如何修复类型“()”不能符合“视图”; 只有结构/枚举/类类型可以符合协议 - how do i fix Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols “类型‘()’不能符合‘视图’; 只有结构/枚举/类类型可以符合协议” - “Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols” type() 不能符合 View; 只有结构/枚举/类类型可以符合协议 - Type () cannot conform to View; only struct/enum/class types can conform to protocols 类型 &#39;()&#39; 不能符合 &#39;View&#39;; 只有 struct/enum/class 类型可以符合使用 swift ui 调用调用函数的协议 - Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols calling calling functions with swift ui Swift 错误:“类型 &#39;()&#39; 不能符合 &#39;View&#39;;只有 struct/enum/class 类型可以符合协议”调用函数写入文本时 - Swift error: "Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols" when calling function to write text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM