英文:
Conflicting arguments to generic parameter Content, RowContent when using a custom view is used inside List and NavigationView
问题
I'm trying to create a simple ToDo App for practice. I created a DataModel for struct Task and tried to use it to iterate in a List in a NavigationView. When I write code like this, it works fine.
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List(DataModel.data, id: \.id) { object in
HStack {
object.finished == true ?
Label(object.description, systemImage: "checkmark.circle") :
Label(object.description, systemImage: "circle")
}
}
}
}
}
I see the UI being rendered like this.
However, when I move the HStack into a custom view called TaskView, I'm getting multiple errors on lines NavigationView and List. The errors are as follows.
Conflicting arguments to generic parameter 'Content' ('<<hole>>' vs. '<<hole>>' vs. '<<hole>>' vs. '<<hole>>')
Conflicting arguments to generic parameter 'Content' ('NavigationView<<<hole>>>' vs. 'NavigationView<<<hole>>>')
Conflicting arguments to generic parameter 'Content' ('ForEach<[Task], Int, <<hole>>>' vs. 'ForEach<[Task], Int, <<hole>>>' vs. 'ForEach<[Task], Int, <<hole>>>' vs. 'ForEach<[Task], Int, <<hole>>>')
Conflicting arguments to generic parameter 'Content' ('List<Never, ForEach<[Task], Int, <<hole>>>>' vs. 'List<Never, ForEach<[Task], Int, <<hole>>>>')
Conflicting arguments to generic parameter 'RowContent' ('<<hole>>' vs. '<<hole>>')
Here is the source code for both the ContentView and TaskView in the failing case.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List(DataModel.data, id: \.id) { object in
TaskView(finished: object.finished, description: object.description)
}
}
}
}
TaskView.swift
import SwiftUI
struct TaskView: View {
@Binding var finished: Bool
@Binding var description: String
var body: some View {
HStack {
finished == true ?
Label(description, systemImage: "checkmark.circle") :
Label(description, systemImage: "circle")
}
}
}
struct TaskView_Previews: PreviewProvider {
@State static var finished = false
@State static var description = "Persimmons"
static var previews: some View {
TaskView(finished: $finished, description: $description)
}
}
DataModel.swift
import UIKit
struct MyTask: Hashable {
var id: Int
var finished: Bool
var description: String
}
class DataModel: NSObject {
static let data: [MyTask] = [
MyTask(id: 0, finished: false, description: "Onions"),
MyTask(id: 1, finished: true, description: "Tomatoes"),
MyTask(id: 2, finished: false, description: "Persimmons"),
MyTask(id: 3, finished: false, description: "Pasta")
]
}
Can anyone tell me why I'm getting this error and how to resolve this error and what is the right way to create a CustomView?
Update
Following change to TaskView seems to have done the trick. Removing @Binding
resolved the errors and the UI is rendered. However, not sure how the changes to datamodel are propagated in that case, but that is for another time.
struct TaskView: View {
var finished: Bool
var description: String
var body: some View {
HStack {
finished == true ?
Label(description, systemImage: "checkmark.circle").foregroundColor(Color.gray) :
Label(description, systemImage: "circle").foregroundColor(Color.black)
}.padding(.leading).padding(.bottom)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
英文:
I'm trying to create a simple ToDo App for practice. I created a DataModel for struct Task and tried to use it to iterate in a List in a NavigationView. When I write code like this, it works fine.
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List(DataModel.data, id: \.id) { object in
HStack {
object.finished == true ?
Label(object.description, systemImage: "checkmark.circle") :
Label(object.description, systemImage: "circle")
}
}
}
}
}
I see the UI being rendered like this.
However, when I move the HStack into a custom view called TaskView, I'm getting multiple errors on lines NavigationView and List. The errors are as follows.
Conflicting arguments to generic parameter 'Content' ('<<hole>>' vs. '<<hole>>' vs. '<<hole>>' vs. '<<hole>>')
Conflicting arguments to generic parameter 'Content' ('NavigationView<<<hole>>>' vs. 'NavigationView<<<hole>>>')
Conflicting arguments to generic parameter 'Content' ('ForEach<[Task], Int, <<hole>>>' vs. 'ForEach<[Task], Int, <<hole>>>' vs. 'ForEach<[Task], Int, <<hole>>>' vs. 'ForEach<[Task], Int, <<hole>>>')
Conflicting arguments to generic parameter 'Content' ('List<Never, ForEach<[Task], Int, <<hole>>>>' vs. 'List<Never, ForEach<[Task], Int, <<hole>>>>')
Conflicting arguments to generic parameter 'RowContent' ('<<hole>>' vs. '<<hole>>')
Here is the source code for both the ContentView and TaskView in failing case.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List(DataModel.data, id: \.id) { object in
TaskView(finished: object.finished, description: object.description)
}
}
}
}
TaskView.swift
import SwiftUI
struct TaskView: View {
@Binding var finished: Bool
@Binding var description: String
var body: some View {
HStack {
finished == true ?
Label(description, systemImage: "checkmark.circle") :
Label(description, systemImage: "circle")
}
}
}
struct TaskView_Previews: PreviewProvider {
@State static var finished = false
@State static var description = "Persimmons"
static var previews: some View {
TaskView(finished: $finished, description: $description)
}
}
DataModel.swift
import UIKit
struct MyTask: Hashable {
var id: Int
var finished: Bool
var description: String
}
class DataModel: NSObject {
static let data: [MyTask] = [
MyTask(id: 0, finished: false, description: "Onions"),
MyTask(id: 1, finished: true, description: "Tomatoes"),
MyTask(id: 2, finished: false, description: "Persimmons"),
MyTask(id: 3, finished: false, description: "Pasta")
]
}
Can any one tell me why I'm getting this error and how to resolve this error and what is the right way to create a CustomView?
Update
Following change to TaskView seems to have done the trick. Removing @Binding
resolved the errors and the UI is rendered. However, not sure how the changes to datamodel are propagated in that case, but that is for another time.
struct TaskView: View {
var finished: Bool
var description: String
var body: some View {
HStack {
finished == true ?
Label(description, systemImage: "checkmark.circle").foregroundColor(Color.gray) :
Label(description, systemImage: "circle").foregroundColor(Color.black)
}.padding(.leading).padding(.bottom)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
答案1
得分: 0
根据评论,对TaskView的以下更改似乎解决了问题。删除@Binding
解决了错误,UI得以渲染。
struct TaskView: View {
var finished: Bool
var description: String
var body: some View {
HStack {
finished == true ?
Label(description, systemImage: "checkmark.circle").foregroundColor(Color.gray) :
Label(description, systemImage: "circle").foregroundColor(Color.black)
}.padding(.leading).padding(.bottom)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
英文:
Based on the the comments, following change to TaskView seems to have done the trick. Removing @Binding
resolved the errors and the UI is rendered.
struct TaskView: View {
var finished: Bool
var description: String
var body: some View {
HStack {
finished == true ?
Label(description, systemImage: "checkmark.circle").foregroundColor(Color.gray) :
Label(description, systemImage: "circle").foregroundColor(Color.black)
}.padding(.leading).padding(.bottom)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
答案2
得分: 0
.sheet(isPresented: $isShowsLikes) {
NavigationView {
List(likeVM.postLikes) { like in
NavigationLink(
destination: NavigateProfileView(isInProfile: $isInProfile, id: post.userid, username: post.username),
label: {
Text(like.username)
}
)
}
.listStyle(.plain)
.padding()
}
}
英文:
.sheet(isPresented: $isShowsLikes) {
NavigationView {
List(likeVM.postLikes) { like in
NavigationLink(
destination: NavigateProfileView(isInProfile: $isInProfile, id: post.userid, username: post.username),
label: {
Text(like.username)
}
)
}
.listStyle(.plain)
.padding()
}
}
I solve my problem with this. A little bit different but using binding in List and inside the NavigationLink was given me same error. With this navigationLink usage i fixed it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论