Как сравнить пользовательские объекты на основе различных свойств, используя Equatable? - PullRequest
1 голос
/ 02 июня 2019

Я использую уравниваемый протокол для сравнения двух пользовательских объектов на основе одного свойства с именем mediaUID.
Есть ли способ переключения между сравнениями по разным свойствам?
В func fetchNotificationsRemoved иногда мне нужно сравнить по mediaUID или likeUID.

 var notificationsArray = [NotificationInformation]()

class NotificationInformation {

    let type: String
    let mediaUID: String?
    let commentUID: String?
    let likeUID:String?    
}


extension NotificationInformation {
    func fetchNotificationsRemoved(query: DatabaseQuery) {

    NotificationInformation.observeNewNotificationsChildRemoved(query: query) { [weak self] (newNotification: NotificationInformation?) in

            guard let strongSelf = self else {return}
            guard let notification = newNotification else {return}

        if notification.type == "like" {

            // How to compare based on likeUID using equatable?
            //compare based on likeUID
            //get the index of the item of type 'like' in notificationsArray and do something with it
            guard let index = strongSelf.notificationsArray.index(of: notification) else {return}

        }else if notification.type == "media" {
            // How to compare based on mediaUID using equatable?
            //compare based on mediaUID
            //get the index of the item of type 'media' in notificationsArray
            guard let index = strongSelf.notificationsArray.index(of: notification) else {return}
        } else if if notification.type == "commentUID" {
           ....
   }


            guard let index = strongSelf.notificationsArray.index(of: notification) else {return}

            strongSelf.notificationsArray.remove(at: index)

            let visibleIndexes = strongSelf.tableView.indexPathsForVisibleRows
            let indexPathOfRemovedNotification = IndexPath(row: index, section: 0)

            if let indexes = visibleIndexes,
                indexes.contains(indexPathOfRemovedNotification) {
                strongSelf.tableView.deleteRows(at: [indexPathOfRemovedNotification], with: .fade)
            }
        }
    }

}//end extension

//enables us to compare two objects of type NotificationInformation
extension NotificationInformation: Equatable { }

func ==(lhs: NotificationInformation ,rhs: NotificationInformation) -> Bool {
    guard let mediaUIDLeft = lhs.mediaUID else {return false}
    guard let mediaUIDRight = rhs.mediaUID else {return false}
    return mediaUIDLeft == mediaUIDRight
}

Ответы [ 3 ]

2 голосов
/ 02 июня 2019

Вы можете использовать static var, чтобы установить поле, которое вы хотите использовать для сравнения:

class NotificationInformation: Equatable {
    enum CompareField {
        case type, mediaUID, commentUID, likeUID
    }

    static var compareField: CompareField = .mediaUID

    let type: String
    let mediaUID: String?
    let commentUID: String?
    let likeUID:String?

    init(type: String, mediaUID: String? = nil, commentUID: String? = nil, likeUID: String? = nil) {
        self.type = type
        self.mediaUID = mediaUID
        self.commentUID = commentUID
        self.likeUID = likeUID
    }

    static func ==(lhs: NotificationInformation, rhs: NotificationInformation) -> Bool {
        switch NotificationInformation.compareField {
        case .type:
            return lhs.type == rhs.type
        case .mediaUID:
            return lhs.mediaUID == rhs.mediaUID
        case .commentUID:
            return lhs.commentUID == rhs.commentUID
        case .likeUID:
            return lhs.likeUID == rhs.likeUID
        }
    }
}

Пример:

let a = NotificationInformation(type: "foo", mediaUID: "123")
let b = NotificationInformation(type: "bar", mediaUID: "123")

NotificationInformation.compareField = .type
if a == b {
    print("same type")
}

NotificationInformation.compareField = .mediaUID
if a == b {
    print("same mediaUID")
}

Выход:

same mediaUID

Сравнение нескольких полей с использованием OptionSet

Если вы замените enum на OptionSet, вы можете выбрать несколько полей для сравнения:

struct CompareFields: OptionSet {
    let rawValue: Int

    static let type       = CompareFields(rawValue: 1 << 0)
    static let mediaUID   = CompareFields(rawValue: 1 << 1)
    static let commentUID = CompareFields(rawValue: 1 << 2)
    static let likeUID    = CompareFields(rawValue: 1 << 3)
}

static var compareFields: CompareFields = .mediaUID

static func ==(lhs: NotificationInformation, rhs: NotificationInformation) -> Bool {
    var equal = true

    if NotificationInformation.compareFields.contains(.type) {
        equal = equal && (lhs.type == rhs.type)
    }
    if NotificationInformation.compareFields.contains(.mediaUID) {
        equal = equal && (lhs.mediaUID == rhs.mediaUID)
    }
    if NotificationInformation.compareFields.contains(.commentUID) {
        equal = equal && (lhs.commentUID == rhs.commentUID)
    }
    if NotificationInformation.compareFields.contains(.likeUID) {
        equal = equal && (lhs.likeUID == rhs.likeUID)
    }

    return equal
}

Пример * +1029 *

let a = NotificationInformation(type: "foo", mediaUID: "123", commentUID: "111")
let b = NotificationInformation(type: "bar", mediaUID: "123", commentUID: "111")

NotificationInformation.compareFields = .mediaUID
if a == b {
    print("same mediaUID")
}

NotificationInformation.compareFields = [.mediaUID, .commentUID]
if a == b {
    print("same mediaUID and commentUID")
}

выход

same mediaUID
same mediaUID and commentUID

Многопоточный выпуск

Существует проблема, если ваш код изменяет значение compareFields в другом потоке. Значение равно будет меняться для всех потоков. Одним из возможных решений является изменение и использование равенства для NotificationInformation в главном потоке.

...
} else if notification.type == "media" {
    DispatchQueue.main.async {
        NotificationInformation.compareFields = .mediaUID

        guard let index = strongSelf.notificationsArray.index(of: notification) else {return}

        // use index
        ...
    }
}
...
1 голос
/ 02 июня 2019

Вы можете проверить свойство type объектов NotificationInformation и сравнить объекты в соответствии с этим.

extension NotificationInformation: Equatable {
    static func == (lhs: NotificationInformation, rhs: NotificationInformation) -> Bool {
        guard lhs.type == rhs.type  else {
            print("Types of lhs and rhs are not same ")
            return false
        }
        switch lhs.type {
        case "like": return lhs.likeUID == rhs.likeUID
        case "media": return lhs.mediaUID == rhs.mediaUID
        case "commentUID": return lhs.commentUID == rhs.commentUID
        default: return false
        }
    }
}


И вы можете использовать enum для type свойство

class NotificationInformation {
    enum NotificationType: String {
        case like
        case media
        case commentUID
    }
    let type: NotificationType
    let mediaUID: String?
    let commentUID: String?
    let likeUID:String?
}

extension NotificationInformation: Equatable {
    static func == (lhs: NotificationInformation, rhs: NotificationInformation) -> Bool {
        guard lhs.type == rhs.type  else {
            print("Types of lhs and rhs are not same ")
            return false
        }
        switch lhs.type {
        case .like: return lhs.likeUID == rhs.likeUID
        case .media: return lhs.mediaUID == rhs.mediaUID
        case .commentUID: return lhs.commentUID == rhs.commentUID
        }
    }
}

Использование

extension NotificationInformation {
    func fetchNotificationsRemoved(query: DatabaseQuery) {
        NotificationInformation.observeNewNotificationsChildRemoved(query: query) { [weak self] newNotification in
            guard let strongSelf = self else {return}
            guard let notification = newNotification else {return}
            guard let index = strongSelf.notificationsArray.index(of: notification) else {return}
            strongSelf.notificationsArray.remove(at: index)
            let visibleIndexes = strongSelf.tableView.indexPathsForVisibleRows
            let indexPathOfRemovedNotification = IndexPath(row: index, section: 0)
            if let indexes = visibleIndexes, indexes.contains(indexPathOfRemovedNotification) {
                strongSelf.tableView.deleteRows(at: [indexPathOfRemovedNotification], with: .fade)
            }
        }
    }
}
1 голос
/ 02 июня 2019

Измените func на это:

          func ==(lhs: NotificationInformation ,rhs: NotificationInformation) -> Bool {
            guard let mediaUIDLeft = lhs.mediaUID else {return false}
            guard let mediaUIDRight = rhs.mediaUID else {return false}

            return (mediaUIDLeft == mediaUIDRight || lhs.likeUID == rhs.likeUID)
         }

Это означает, что два NotificationInformation равны, если они имеют одинаковые mediaUID ИЛИ одинаковые likeUID

Если вам нужна условная проверка, вы можете ввести логическую переменную:

    class NotificationInformation {

      let type: String
      let mediaUID: String?
      let commentUID: String?
      let likeUID:String?    

      let checkByMediaUID: Bool = true

    }

Так что поменяйте Equatable:

      func ==(lhs: NotificationInformation ,rhs: NotificationInformation) -> Bool {
          guard let mediaUIDLeft = lhs.mediaUID else {return false}
          guard let mediaUIDRight = rhs.mediaUID else {return false}

          return (lhs.checkByMediaUID || rhs.checkByMediaUID) ? mediaUIDLeft == mediaUIDRight : lhs.likeUID == rhs.likeUID
      }

более читабельным способом:

   func ==(lhs: NotificationInformation ,rhs: NotificationInformation) -> Bool {
          guard let mediaUIDLeft = lhs.mediaUID else {return false}
          guard let mediaUIDRight = rhs.mediaUID else {return false}

          if lhs.checkByMediaUID || rhs.checkByMediaUID{
              return mediaUIDLeft == mediaUIDRight
          }

          return lhs.likeUID == rhs.likeUID
      }

Это означает, что если вы хотите проверить по mediaUID, просто сравните два объекта. Если вы хотите проверить по likeUID, просто измените переменную одного из объектов.

* ** тысяча двадцать-восемь ** тысячи двадцать-девять +1030 * Пример * +1033 *
     let a: NotificationInformation = NotificationInformation()
     let b: NotificationInformation = NotificationInformation()

     //Check by `mediaUID`
     if a == b{
        ....
     }

     //Check by `likeUID`
     a.checkByMediaUID = false

     if a == b{
        ....
     }
...