错误:传递给不带参数 SwiftUI 的调用的参数
Posted
技术标签:
【中文标题】错误:传递给不带参数 SwiftUI 的调用的参数【英文标题】:Error: Argument passed to call that takes no arguments SwiftUI 【发布时间】:2020-10-30 14:42:06 【问题描述】:在我的代码中,我收到了这个错误,我不知道如何解决它。错误在尾随代码块中
struct Map: View
@ObservedObject public var dataManager = DataManager.shared
@Binding public var seqId: String
// rz- to pass seqId to second view
// @State public var seqId2: String = ""
var body: some View
ZStack
VStack
Text("Results").foregroundColor(Color.white).onAppearself.dataManager.downloadSeqData(seqId: self.seqId) .onAppear(perform: playSound)/*rz want to add this sound func to contentview somehow, otherwise it will constantly play sound when page loads */ .edgesIgnoringSafeArea(.all)
//rz- if strand is linear draw line
if dataManager.dataSet?.INSDSeq.topology == "linear"
Rectangle()
.fill(Color.black)
.frame(width: 500, height: 20)
//rz- if strand is circular draw o
if dataManager.dataSet?.INSDSeq.topology == "circular"
Circle()
.stroke(Color.black, style: StrokeStyle(lineWidth: 20, lineCap: .round, lineJoin: .round))
.frame(width: 500, height: 500)
if dataManager.dataSet?.INSDSeq.topology != "circular" && dataManager.dataSet?.INSDSeq.topology != "linear"
Text("Data Unavailable, try a new accession number.")
.navigationBarItems(
//rz - added home and genbank view to navigation bars
leading:
NavigationLink(destination: ContentView())
Text("New Query")
.font(.title)
.foregroundColor(Color.black)
,
trailing:
NavigationLink(destination: Screen(seqId: self.$seqId))
Text("GenBank View")
.font(.title)
.foregroundColor(Color.black)
错误在$seqId
NavigationLink(destination: Screen(seqId: self.$seqId))
这是屏幕视图
import SwiftUI
struct Screen: View
@ObservedObject public var dataManager = DataManager.shared
@State private var seqId: String = ""
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)
struct IntervalSection: View
var feature: INSDFeature
var body: some View
VStack (alignment: .leading)
ForEach(0..<feature.INSDFeature_intervals.count, id: \.self) i in
ForEach(0..<feature.INSDFeature_intervals[i].INSDInterval.count, id: \.self) j in
if let from = feature.INSDFeature_intervals[i].INSDInterval[j].INSDInterval_from
VStack (alignment: .leading)
Text("\(from)").bold()
Text("\(feature.INSDFeature_intervals[i].INSDInterval[j].INSDInterval_to ?? -1)")
.padding()
.cornerRadius(10)
struct QualsSection: View
var feature: INSDFeature
var body: some View
VStack (alignment: .leading)
ForEach(0..<feature.INSDFeature_quals.count , id: \.self) i in
ForEach(0..<feature.INSDFeature_quals[i].INSDQualifier.count , id: \.self) j in
VStack (alignment: .leading)
Text(feature.INSDFeature_quals[i].INSDQualifier[j].INSDQualifier_name).bold()
Text(feature.INSDFeature_quals[i].INSDQualifier[j].INSDQualifier_value)
.padding()
.cornerRadius(10)
【问题讨论】:
Screen
是什么?对于Screen
,init
方法是什么样的?很可能你没有这样的 init。
Screen 是以列表格式显示信息的内容视图。屏幕页面中没有init方法
贴代码太麻烦?它有什么属性,如果它是一个我认为是的结构,总是有一个合成的 init。
刚刚上传了
并且视图不采用 seqId 作为参数,因为该属性是私有的(并且是 @State
变量)。在我看来,屏幕视图不需要用 seqId 初始化,因为它将它作为输入?否则你需要将其更改为@Binding
并删除私有
【参考方案1】:
在 SwiftUI 中,如果数据来自另一个视图,则应使用 Binding
而不是 State
。此外,Screen
的两个属性都是私有的,因此在这种情况下无法从结构本身的外部访问它们。
我建议将Screen
中的seqID
公开,就像您使用dataManager
所做的那样,并将其更改为State
【讨论】:
以上是关于错误:传递给不带参数 SwiftUI 的调用的参数的主要内容,如果未能解决你的问题,请参考以下文章
尝试将某些位置固定在 MapView 上时传递给不带参数的调用的参数
Swift Vision Framework - VNRecognizeTextRequest:传递给不带参数的调用的参数