繁体   English   中英

SwiftUI 错误修复“无法转换类型‘绑定’的值<int> &#39;到预期类型&#39;绑定&lt;_&gt;?&#39;”

[英]SwiftUI Bug Fix "Cannot convert value of type 'Binding<int>'to expected type 'Binding<_>?'"

我遇到了这个错误的问题:

“无法将 'Binding' 类型的值转换为预期类型 'Binding<_>?'”

我想我正在使用过时的 SwiftUI 版本进行编码,但我不是 100% 确定,所以我能得到的任何帮助都会很棒。

我在下面显示了我的代码,以便您可以查看它。

我不确定底部是否重要,但我添加它只是为了安全。

内容视图

import SwiftUI

struct ContentView: View {
    @State private var selection = 0
    @State var networkManager = NetworkManager()
    var body: some View {
        TabView(selection: $selection){

            NavigationView{
            Text("First View")
                .font(.title)
                    .navigationBarTitle(Text("Welcome"))
                }
                .tabItem {
                    VStack {
                        Image(systemName: "star.fill")
                        Text("Welcome")
                    }
                }
                .tag(0)
            NavigationView{

                List(networkManager.featureList.results.identified(by: \.url)) { featured in
                Text(featured.name.capitalized)
                }
                    .navigationBarTitle(Text("Featured"))
                }
                .tabItem {
                    VStack {
                        Image(systemName: "app.badge.fill")
                        Text("Featured")
                    }
                }
                .tag(1)
            NavigationView{
            Text("First View")
                .font(.title)
                    .navigationBarTitle(Text("Repos"))
                }
            .tabItem {
                VStack {
                    Image(systemName: "rectangle.stack.fill")
                    Text("Repos")
                }
            }
            .tag(2)
            NavigationView{
            Text("First View")
                .font(.title)
                    .navigationBarTitle(Text("Request"))
                }
            .tabItem {
                VStack {
                    Image(systemName: "icloud.and.arrow.down.fill")
                    Text("Request")
                }
            }
            .tag(3)
        }
    }
}

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

接口视图

import Foundation
import SwiftUI
import Combine

class NetworkManager: ObservableObject {
    var didChange = PassthroughSubject<NetworkManager, Never>()
    var featureList = FeaturedApiList(results: []){
        didSet{
            didChange.send(self)
        }
    }

    init(){
        guard let url = URL(string: "https://myurl.com/repos.json") else { return }
        URLSession.shared.dataTask(with: url) { (data, _, _) in
            guard let data = data else { return }
            let featureList = try! JSONDecoder().decode(FeaturedApiList.self, from: data)
            DispatchQueue.main.async {
                self.featureList = featureList

            }
        }.resume()
    }
}

谢谢!

显示的错误非常令人困惑,但其他一些错误会导致此类错误。

在您的情况下,您可能需要修复TabView的第二个NavigationView

            NavigationView{
                //↓Fix this line.
                List(networkManager.featureList.results, id: \.url) { featured in
                    Text(featured.name.capitalized)
                }
                .navigationBarTitle(Text("Featured"))
            }
            .tabItem {
                VStack {
                    Image(systemName: "app.badge.fill")
                    Text("Featured")
                }
            }
            .tag(1)

最好检查此线程并始终尝试查找最新示例或教程。

欢迎使用 Stackoverflow!

OOPer 实际上是正确的。 您将需要修复您的List 让我们尝试为List替换您的数据,只是为了查看项目将编译:

假设我们有一个符合Identifiable协议的模型,如下所示:

struct Person: Identifiable {
    var id = UUID()
    var name: String
}

然后用这个替换你的List行:

List([Person(name: "fafa")]) { featured in
    Text(featured.name)
}

这次它应该运行。 由于SwiftUI是新的,这一定是错误相当混乱的原因。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM