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:
- Update the
Screenview's property declaration: Swap the private@Statevariable for a public@Bindingvariable. This tells SwiftUI the value is managed externally (by theMapview). - Remove the default value—the binding will be supplied from
Mapwhen initializingScreen. - Keep using
$seqIdin the TextField as before—it works seamlessly with aBinding.
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,Screennow shares the sameseqIdvalue asMap—any changes made inScreen's TextField will automatically sync back toMap. - The
NavigationLinkinMap(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:
- Keep
@State private var seqId: String = ""inScreen - Add an initializer:
init(initialSeqId: String) { _seqId = State(initialValue: initialSeqId) } - Call it from
Maplike 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

