CLLocation 是如何实现 Equatable 协议的呢?
How does CLLocation implement the Equatable protocol?
在回答关于SO的另一个问题时,我发现CLLocation
class符合Equatable
协议。它用什么方法判断是否相等?
lat/long 完全匹配? lat/long 和高度完全匹配?纬度、经度、高度和时间戳的精确匹配?速度和航向如何?仅使用 lat/long 对创建的 CLLocation
对象呢?该位置的各种其他值不是可选的,那么使用 init(latitude:longitude:)
创建的位置的海拔高度是多少?
CLLocation class 与任何符合 Equatable 的 class 非常相似,实现了 (==) 运算符
为了回答您的其他问题,我决定使用此代码创建一个游乐场
import UIKit
import CoreLocation
var str = "Hello, playground"
var coordinate = CLLocationCoordinate2D.init(latitude: 42.0, longitude: 42.0)
var accuracy = CLLocationAccuracy.init(24.0)
var date = Date.init(timeIntervalSinceNow: 0)
var loc1 = CLLocation.init(coordinate: coordinate, altitude: 44.0, horizontalAccuracy: accuracy, verticalAccuracy: accuracy, timestamp: date)
var loc2 = CLLocation.init(coordinate: coordinate, altitude: 44.0, horizontalAccuracy: accuracy, verticalAccuracy: accuracy, timestamp: date)
var loc3 = CLLocation.init(latitude: 42.0, longitude: 42.0)
var loc4 = CLLocation.init(latitude: 42.0, longitude: 42.0)
var loc5 = CLLocation.init(coordinate: coordinate, altitude: 44.0, horizontalAccuracy: accuracy, verticalAccuracy: accuracy, course: .infinity, speed: 55.0, timestamp: date)
var loc6 = CLLocation.init(coordinate: coordinate, altitude: 44.0, horizontalAccuracy: accuracy, verticalAccuracy: accuracy, course: .infinity, speed: 55.0, timestamp: date)
var bool1 = loc1 == loc2 //false
var bool2 = loc2 == loc3 //false
var bool3 = loc2 == loc2 //true
var bool4 = loc1 == loc4 //false
var bool5 = loc5 == loc6 //false
唯一得出 TRUE 的布尔值是 bool3。
因此,无论不同 CLLocation 对象上的各个属性是否相同,== 运算符都不会将对象视为相等。我猜比较位置的最佳方法是比较您感兴趣的 CLLocation 对象的字段
How does CLLocation
implement the Equatable protocol?
没有。没有用于比较两个 CLLocation
实例的重写 ==
函数。使用两个 CLLocation
实例调用 ==
时,使用 NSObject
==
函数:
public func ==(lhs: NSObject, rhs: NSObject) -> Bool
要实际比较两个 CLLocation
实例,请比较您关心的每个实例的属性(纬度或经度),或者对两个位置使用内置的 distance(from:)
方法并将其与CLLocationDistance
阈值。
完全验证JAL在他的回答中所说的内容,我写道:
import Foundation
import UIKit
import CoreLocation
class ViewController: UIViewController{
var cl1 = CLLocation()
var cl2 = CLLocation()
override func viewDidLoad() {
super.viewDidLoad()
if cl1 == cl2{
}
}
}
然后我命令点击 ==
(来自 if cl1 == cl2
)。我花了:
extension NSObject : CVarArg {
}
public func ==(lhs: Selector, rhs: Selector) -> Bool
public func ==(lhs: NSObject, rhs: NSObject) -> Bool
public struct NSZone {
}
为了仔细检查,我命令单击 CLLocation
并看到:
open class CLLocation : NSObject, NSCopying, NSSecureCoding {
...
}
所以基本上 ==
是因为它是 NSObject
的子类,后者仅比较引用。
在回答关于SO的另一个问题时,我发现CLLocation
class符合Equatable
协议。它用什么方法判断是否相等?
lat/long 完全匹配? lat/long 和高度完全匹配?纬度、经度、高度和时间戳的精确匹配?速度和航向如何?仅使用 lat/long 对创建的 CLLocation
对象呢?该位置的各种其他值不是可选的,那么使用 init(latitude:longitude:)
创建的位置的海拔高度是多少?
CLLocation class 与任何符合 Equatable 的 class 非常相似,实现了 (==) 运算符
为了回答您的其他问题,我决定使用此代码创建一个游乐场
import UIKit
import CoreLocation
var str = "Hello, playground"
var coordinate = CLLocationCoordinate2D.init(latitude: 42.0, longitude: 42.0)
var accuracy = CLLocationAccuracy.init(24.0)
var date = Date.init(timeIntervalSinceNow: 0)
var loc1 = CLLocation.init(coordinate: coordinate, altitude: 44.0, horizontalAccuracy: accuracy, verticalAccuracy: accuracy, timestamp: date)
var loc2 = CLLocation.init(coordinate: coordinate, altitude: 44.0, horizontalAccuracy: accuracy, verticalAccuracy: accuracy, timestamp: date)
var loc3 = CLLocation.init(latitude: 42.0, longitude: 42.0)
var loc4 = CLLocation.init(latitude: 42.0, longitude: 42.0)
var loc5 = CLLocation.init(coordinate: coordinate, altitude: 44.0, horizontalAccuracy: accuracy, verticalAccuracy: accuracy, course: .infinity, speed: 55.0, timestamp: date)
var loc6 = CLLocation.init(coordinate: coordinate, altitude: 44.0, horizontalAccuracy: accuracy, verticalAccuracy: accuracy, course: .infinity, speed: 55.0, timestamp: date)
var bool1 = loc1 == loc2 //false
var bool2 = loc2 == loc3 //false
var bool3 = loc2 == loc2 //true
var bool4 = loc1 == loc4 //false
var bool5 = loc5 == loc6 //false
唯一得出 TRUE 的布尔值是 bool3。
因此,无论不同 CLLocation 对象上的各个属性是否相同,== 运算符都不会将对象视为相等。我猜比较位置的最佳方法是比较您感兴趣的 CLLocation 对象的字段
How does
CLLocation
implement the Equatable protocol?
没有。没有用于比较两个 CLLocation
实例的重写 ==
函数。使用两个 CLLocation
实例调用 ==
时,使用 NSObject
==
函数:
public func ==(lhs: NSObject, rhs: NSObject) -> Bool
要实际比较两个 CLLocation
实例,请比较您关心的每个实例的属性(纬度或经度),或者对两个位置使用内置的 distance(from:)
方法并将其与CLLocationDistance
阈值。
完全验证JAL在他的回答中所说的内容,我写道:
import Foundation
import UIKit
import CoreLocation
class ViewController: UIViewController{
var cl1 = CLLocation()
var cl2 = CLLocation()
override func viewDidLoad() {
super.viewDidLoad()
if cl1 == cl2{
}
}
}
然后我命令点击 ==
(来自 if cl1 == cl2
)。我花了:
extension NSObject : CVarArg {
}
public func ==(lhs: Selector, rhs: Selector) -> Bool
public func ==(lhs: NSObject, rhs: NSObject) -> Bool
public struct NSZone {
}
为了仔细检查,我命令单击 CLLocation
并看到:
open class CLLocation : NSObject, NSCopying, NSSecureCoding {
...
}
所以基本上 ==
是因为它是 NSObject
的子类,后者仅比较引用。