You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI错误:向无参调用传参问题及代码排查求助

Hey there, let's get that error sorted out for you!

The Root Cause

Your Map view is trying to pass a Binding<String> ($seqId) to the Screen view, but right now Screen doesn't have an initializer that accepts this parameter. The seqId in Screen is declared as a private @State variable, which means it can't receive external bindings by default—hence the "no arguments expected" error when you try to pass $seqId.

The Fix (Matching Your Goal)

Since you want to pass and sync the seqId between Map and Screen, we need to adjust the Screen view to accept a Binding<String> instead of using a private local state. Here's how:

  1. Update the Screen view's property declaration: Swap the private @State variable for a public @Binding variable. This tells SwiftUI the value is managed externally (by the Map view).
  2. Remove the default value—the binding will be supplied from Map when initializing Screen.
  3. Keep using $seqId in the TextField as before—it works seamlessly with a Binding.

Modified Screen View Code

import SwiftUI
struct Screen: View {
    @ObservedObject public var dataManager = DataManager.shared
    @Binding public var seqId: String // Changed from @State private to @Binding public
    
    var body: some View {
        VStack {
            TextField("Enter Accession Number", text: $seqId)
                .padding()
            Button("Search") {
                dataManager.downloadSeqData(seqId: seqId)
            }
            ScrollView {
                VStack(alignment: .leading) {
                    Text(dataManager.dataSet?.INSDSeq.locus ?? "").padding()
                    Text(dataManager.dataSet?.INSDSeq.organism ?? "").padding()
                    Text(dataManager.dataSet?.INSDSeq.source ?? "").padding()
                    Text(dataManager.dataSet?.INSDSeq.taxonomy ?? "").padding()
                    Text(dataManager.dataSet?.INSDSeq.topology ?? "").padding()
                    Text(dataManager.dataSet?.INSDSeq.length == nil ? "" : "\(dataManager.dataSet!.INSDSeq.length)").padding()
                    Text(dataManager.dataSet?.INSDSeq.strandedness ?? "").padding()
                    Text(dataManager.dataSet?.INSDSeq.moltype ?? "").padding()
                    Text(dataManager.dataSet?.INSDSeq.sequence ?? "").padding()
                }.cornerRadius(10)
                VStack(alignment: .leading, spacing: 10) {
                    ForEach(dataManager.featureList, id:\.self) { feature in
                        VStack (alignment: .leading){
                            VStack (alignment: .leading) {
                                Text(feature.INSDFeature_key).bold()
                                Text(feature.INSDFeature_location)
                            }.padding()
                            IntervalSection(feature: feature)
                            QualsSection(feature: feature)
                        }.cornerRadius(10)
                    }
                }.cornerRadius(20)
            }.padding(.horizontal)
        }
    }
}

Why This Works

  • By using @Binding, Screen now shares the same seqId value as Map—any changes made in Screen's TextField will automatically sync back to Map.
  • The NavigationLink in Map (Screen(seqId: self.$seqId)) now matches a valid initializer, so the error disappears.

Optional: If You Want Screen to Have Its Own Local State

If you only need to pass an initial value (not keep it synced), you could instead:

  1. Keep @State private var seqId: String = "" in Screen
  2. Add an initializer:
    init(initialSeqId: String) {
        _seqId = State(initialValue: initialSeqId)
    }
    
  3. Call it from Map like this: Screen(initialSeqId: self.seqId) (no $ symbol, since we're passing a regular String)

But based on your comment ("将seqId传递至第二个视图"), the first approach using @Binding is exactly what you need for syncing the value between views.

内容的提问来源于stack exchange,提问作者christopher richardson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 17:43:01