Conflicting arguments to generic parameter Content, RowContent when using a custom view is used inside List and NavigationView

huangapple go评论57阅读模式
英文:

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.

Conflicting arguments to generic parameter Content, RowContent when using a custom view is used inside List and NavigationView

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: &quot;checkmark.circle&quot;) :
                    Label(object.description, systemImage: &quot;circle&quot;)
                }
            }
        }
    }
}

I see the UI being rendered like this.

Conflicting arguments to generic parameter Content, RowContent when using a custom view is used inside List and NavigationView

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 &#39;Content&#39; (&#39;&lt;&lt;hole&gt;&gt;&#39; vs. &#39;&lt;&lt;hole&gt;&gt;&#39; vs. &#39;&lt;&lt;hole&gt;&gt;&#39; vs. &#39;&lt;&lt;hole&gt;&gt;&#39;)

Conflicting arguments to generic parameter &#39;Content&#39; (&#39;NavigationView&lt;&lt;&lt;hole&gt;&gt;&gt;&#39; vs. &#39;NavigationView&lt;&lt;&lt;hole&gt;&gt;&gt;&#39;)

Conflicting arguments to generic parameter &#39;Content&#39; (&#39;ForEach&lt;[Task], Int, &lt;&lt;hole&gt;&gt;&gt;&#39; vs. &#39;ForEach&lt;[Task], Int, &lt;&lt;hole&gt;&gt;&gt;&#39; vs. &#39;ForEach&lt;[Task], Int, &lt;&lt;hole&gt;&gt;&gt;&#39; vs. &#39;ForEach&lt;[Task], Int, &lt;&lt;hole&gt;&gt;&gt;&#39;)

Conflicting arguments to generic parameter &#39;Content&#39; (&#39;List&lt;Never, ForEach&lt;[Task], Int, &lt;&lt;hole&gt;&gt;&gt;&gt;&#39; vs. &#39;List&lt;Never, ForEach&lt;[Task], Int, &lt;&lt;hole&gt;&gt;&gt;&gt;&#39;)

Conflicting arguments to generic parameter &#39;RowContent&#39; (&#39;&lt;&lt;hole&gt;&gt;&#39; vs. &#39;&lt;&lt;hole&gt;&gt;&#39;)

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: &quot;checkmark.circle&quot;) :
                Label(description, systemImage: &quot;circle&quot;)
        }
    }
}

struct TaskView_Previews: PreviewProvider {
    
    @State static var finished = false
    @State static var description = &quot;Persimmons&quot;
    
    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: &quot;Onions&quot;),
        MyTask(id: 1, finished: true, description: &quot;Tomatoes&quot;),
        MyTask(id: 2, finished: false, description: &quot;Persimmons&quot;),
        MyTask(id: 3, finished: false, description: &quot;Pasta&quot;)
    ]
}

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: &quot;checkmark.circle&quot;).foregroundColor(Color.gray) :
                Label(description, systemImage: &quot;circle&quot;).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: &quot;checkmark.circle&quot;).foregroundColor(Color.gray) :
                Label(description, systemImage: &quot;circle&quot;).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.

huangapple
  • 本文由 发表于 2023年6月5日 13:58:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定