📁
til
  • Today I Learned
  • Bookmarks
    • Bookmarks
  • Centos
    • How to limit bandwidth on CentOS
  • CI/CD
    • Add a mac as a GitLab runner
    • Setup CI pipeline for iOS projects on gitlab.com
  • cocoapods
    • private-spec-repo
    • CocoaPods save project.pbxproj file in XML plist format on Xcode 7.3.1
  • git
    • Git keeps asking password after El Capitan Upgrade
    • change-case-sensitivity-of-filename
    • interactive-rebase
    • Fork a repo
  • hacking
    • Decompile Android apk
  • homebrew
    • Bash Completion
  • ios
    • ats
    • Failed to open Xcode (LSOpenURLsWithRole() failed with error -10699)
    • Redirect to Settings Page
    • Retrieve expiry date of Provisioning Profile Certificate from .ipa
    • WKWebView set custom HTTP headers
    • IAP applicationUsername is nil
  • Jenkins
    • Create stage dynamically in declarative pipeline
  • mac
    • Catalina failed to sync iPhone with Finder
  • networking
    • shadowsocks vs. VPN
  • Objective-C
    • Keep subview in Scroll View always on screen
    • Custom View using xib
    • Scroll up TextField when keyboard shows
    • autolayout-hugging-vs-resistance
  • Regex
    • regex-chinese-char
  • SQL
    • update-json-value-in-postgresql
    • select-random-row-in-sql
  • SSH
    • verify-ssh-passphrase
  • SVN
    • Svn Checkout Directories only
  • swift
    • equatable
  • unix
    • Create and Grant Sudo Privileges to user
    • Create and Change Current Directory
    • Show ASCII Text welcome message when login with SSH
  • vim
    • Vim with Multiple Files
Powered by GitBook
On this page
  • Implement Equatable protocol with Inheritance
  • Solution
  • References

Was this helpful?

  1. swift

equatable

Implement Equatable protocol with Inheritance

To implement the equatable protocol is as simple as follow:

class SimpleClass: Equatable {
    var propA: String?
    var propB: Int?
    public static func == (lhs: SimpleClass, rhs: SimpleClass) -> Bool {
        return lhs.propA == rhs.propA
            && lhs.propB == rhs.propB
    }
}

But the problem is, if you have an subclass, it is not going to work.

class SimpleSubClass: SimpleClass {
    var propC: String?

    public static func == (lhs: SimpleSubClass, rhs: SimpleSubClass) -> Bool {
        return lhs.propC == rhs.propC
    }
}

Because when you comparing two SimpleSubClass objects, only the super class's == function is called.

Solution

Add an extra equals function that can be overriden in subclass

class SimpleClass: Equatable {
    var propA: String?
    var propB: Int?
    public static func == (lhs: SimpleClass, rhs: SimpleClass) -> Bool {
        return lhs.equals(rhs: rhs)
    }

    func equals(rhs: SimpleClass) -> Bool {
        return self.propA == rhs.propA
            && self.propB == rhs.propB
    }
}

class SimpleSubClass: SimpleClass {
    var propC: String?

    public override func equals(rhs: SimpleClass) -> Bool {
        guard let other = rhs as? SimpleSubClass else { return false }
        return super.equals(rhs: other)
            && self.propC == other.propC
    }
}

References

PreviousSvn Checkout Directories onlyNextCreate and Grant Sudo Privileges to user

Last updated 5 years ago

Was this helpful?

https://forums.swift.org/t/implement-equatable-protocol-in-a-class-hierarchy/13844
https://gist.github.com/fjfdeztoro/a5097f1b24379e127674eb1df8c97d96