Search code examples
iosswiftcllocationequatable

How does CLLocation implement the Equatable protocol?


In answering another question on SO, I found that the CLLocation class conforms to the Equatable protocol. What method does it use to determine equality?

Exact match of lat/long? Exact match of lat/long and altitude? Exact match of latitude, longitude, altitude, and timestamp? What about speed and course? What about CLLocation objects that were created with only a lat/long pair? Various other values of the location are not optionals, so what would the altitude be for a location created using init(latitude:longitude:)?


Solution

  • Just fully verify what JAL has said in his answer, I wrote:

    import Foundation
    import UIKit
    import CoreLocation
    
    class ViewController: UIViewController{
    
        var cl1 = CLLocation()
        var cl2 = CLLocation()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            if cl1 == cl2{
    
            }
        }
    }
    

    Then I command clicked on the == (from if cl1 == cl2). It took me to:

    extension NSObject : CVarArg {
    }
    
    public func ==(lhs: Selector, rhs: Selector) -> Bool
    
    public func ==(lhs: NSObject, rhs: NSObject) -> Bool
    
    public struct NSZone {
    }
    

    To double check I command clicked on CLLocation and saw:

    open class CLLocation : NSObject, NSCopying, NSSecureCoding {
    ...
    }
    

    So basically the == is because it's subclassed from NSObject which only compares references.