如何使绑定值符合 SwiftUI 中的 Equatable
How to make a binding value conform to Equatable in SwiftUI
当我尝试比较任务修饰符中的值时出现以下错误。
运算符函数'=='要求'Binding'符合'Equatable'
如何让 Binding 确认为 Equatable?
非绑定的普通 NotificationDetail 已经确认为 Equatable。
@ObservedObject var notificationsViewModel = NotificationsViewModel.shared
//NotificationsViewModel does a API call and puts the fetched data in the Notifications Model
var body: some View {
VStack {
ForEach($notificationsViewModel.notifications.notificationsDetails) { $notificationsDetail in
VStack(alignment: .leading) {
if notificationsDetail.notificationsCategoriesId == 2 {
NotificationsFriendRequestCell(notificationsDetail: $notificationsDetail, viewModel: viewModel, notificationsViewModel: notificationsViewModel, tabSelection: $tabSelection) //ReceiveFriendRequestCell(user: user, viewModel: viewModel)
}
else if notificationsDetail.notificationsCategoriesId == 3 {
NotificationsFriendRequestApprovedCell(notificationsDetail: $notificationsDetail, viewModel: viewModel, notificationsViewModel: notificationsViewModel, tabSelection: $tabSelection)
}
}
.task { //task modifier for infinite scrolling
//BELOW LINE GETTING ERRROS
if $notificationsDetail == $notificationsViewModel.notifications.notificationsDetails.last {
print("task modifier last item")
numberOfSkips += 10
notificationsViewModel.getMyNotifications(isOnlyUnread: false, skip: numberOfSkips, limit: 10)
}
}
} //ForEach end
} //VStack end
型号
struct Notifications: Codable, Identifiable {
let id = UUID()
let numberOfNotifications: Int
var notificationsDetails: [NotificationsDetail]
enum CodingKeys: String, CodingKey {
case numberOfNotifications = "number_of_notifications"
case notificationsDetails = "notifications"
}
}
struct NotificationsDetail: Codable, Identifiable, Equatable {
let id: Int
let notificationsCategoriesId: Int
let questionsUsersName: String?
enum CodingKeys: String, CodingKey {
case id = "notifications_id"
case notificationsCategoriesId = "notifications_categories_id"
case questionsUsersName = "questions_users_name"
}
static var example = NotificationsDetail(id: 0, notificationsCategoriesId: 1, questionsId: 1, questionsUsersName: "")
init(id: Int, notificationsCategoriesId: Int, questionsUsersName: String?) {
self.id = id
self.notificationsCategoriesId = notificationsCategoriesId
self.questionsUsersName = questionsUsersName
}
}
视图模型
class NotificationsViewModel: ObservableObject {
static let shared = NotificationsViewModel()
@Published var notifications: Notifications = Notifications.example
init(){
A()
}
func A () {
//do API calls and put data in @Published var notifications
}
我尝试通过删除或添加 $ 标记来调整值,但出现其他错误...
if notificationsDetail == notificationsViewModel.notifications.notificationsDetails.last
//Error: Reference to captured var 'notificationsDetail' in concurrently-executing code
if $notificationsDetail == notificationsViewModel.notifications.notificationsDetails.last
//Error: Binary operator '==' cannot be applied to operands of type 'Binding<[NotificationsDetail]>.Element' (aka 'Binding<NotificationsDetail>') and '((NotificationsDetail) throws -> Bool) throws -> NotificationsDetail?'
如有任何建议,我们将不胜感激!
提前致谢。
错误的原因是您正在通过 ForEach
向闭包传递一个绑定,但您想与该绑定的值进行比较。通过 $
和 .wrappedValue
访问它应该在这里工作。因此,将您的代码更改为:
if $notificationsDetail.wrappedValue == notificationsViewModel.notifications.notificationsDetails.last
当我尝试比较任务修饰符中的值时出现以下错误。
运算符函数'=='要求'Binding'符合'Equatable'
如何让 Binding 确认为 Equatable? 非绑定的普通 NotificationDetail 已经确认为 Equatable。
@ObservedObject var notificationsViewModel = NotificationsViewModel.shared
//NotificationsViewModel does a API call and puts the fetched data in the Notifications Model
var body: some View {
VStack {
ForEach($notificationsViewModel.notifications.notificationsDetails) { $notificationsDetail in
VStack(alignment: .leading) {
if notificationsDetail.notificationsCategoriesId == 2 {
NotificationsFriendRequestCell(notificationsDetail: $notificationsDetail, viewModel: viewModel, notificationsViewModel: notificationsViewModel, tabSelection: $tabSelection) //ReceiveFriendRequestCell(user: user, viewModel: viewModel)
}
else if notificationsDetail.notificationsCategoriesId == 3 {
NotificationsFriendRequestApprovedCell(notificationsDetail: $notificationsDetail, viewModel: viewModel, notificationsViewModel: notificationsViewModel, tabSelection: $tabSelection)
}
}
.task { //task modifier for infinite scrolling
//BELOW LINE GETTING ERRROS
if $notificationsDetail == $notificationsViewModel.notifications.notificationsDetails.last {
print("task modifier last item")
numberOfSkips += 10
notificationsViewModel.getMyNotifications(isOnlyUnread: false, skip: numberOfSkips, limit: 10)
}
}
} //ForEach end
} //VStack end
型号
struct Notifications: Codable, Identifiable {
let id = UUID()
let numberOfNotifications: Int
var notificationsDetails: [NotificationsDetail]
enum CodingKeys: String, CodingKey {
case numberOfNotifications = "number_of_notifications"
case notificationsDetails = "notifications"
}
}
struct NotificationsDetail: Codable, Identifiable, Equatable {
let id: Int
let notificationsCategoriesId: Int
let questionsUsersName: String?
enum CodingKeys: String, CodingKey {
case id = "notifications_id"
case notificationsCategoriesId = "notifications_categories_id"
case questionsUsersName = "questions_users_name"
}
static var example = NotificationsDetail(id: 0, notificationsCategoriesId: 1, questionsId: 1, questionsUsersName: "")
init(id: Int, notificationsCategoriesId: Int, questionsUsersName: String?) {
self.id = id
self.notificationsCategoriesId = notificationsCategoriesId
self.questionsUsersName = questionsUsersName
}
}
视图模型
class NotificationsViewModel: ObservableObject {
static let shared = NotificationsViewModel()
@Published var notifications: Notifications = Notifications.example
init(){
A()
}
func A () {
//do API calls and put data in @Published var notifications
}
我尝试通过删除或添加 $ 标记来调整值,但出现其他错误...
if notificationsDetail == notificationsViewModel.notifications.notificationsDetails.last
//Error: Reference to captured var 'notificationsDetail' in concurrently-executing code
if $notificationsDetail == notificationsViewModel.notifications.notificationsDetails.last
//Error: Binary operator '==' cannot be applied to operands of type 'Binding<[NotificationsDetail]>.Element' (aka 'Binding<NotificationsDetail>') and '((NotificationsDetail) throws -> Bool) throws -> NotificationsDetail?'
如有任何建议,我们将不胜感激! 提前致谢。
错误的原因是您正在通过 ForEach
向闭包传递一个绑定,但您想与该绑定的值进行比较。通过 $
和 .wrappedValue
访问它应该在这里工作。因此,将您的代码更改为:
if $notificationsDetail.wrappedValue == notificationsViewModel.notifications.notificationsDetails.last