按协议对两个对象进行比较/相等

时间:2022-05-19 04:30:03

Is there a way to compare two Objective-C objects based purely on the protocol they implement.

是否有一种方法可以仅基于它们实现的协议来比较两个Objective-C对象。

Specifically I'm looking at comparing two objects conforming to MKAnnotation (iPhone mapkit annotations). Given two objects that conform to the protocol, I would like to determine if they are equal as far as the protocol is concerned. In this case, that would mean that at least the coordinate attribute is the same.

具体地说,我正在比较符合MKAnnotation (iPhone mapkit注解)的两个对象。给定两个符合协议的对象,就协议而言,我想确定它们是否相等。在这种情况下,这意味着至少坐标属性是相同的。

1 个解决方案

#1


2  

Since CLLocationCoordinate2D is a struct, you can compare the coordinate @properties of two MKAnnotations with ==. Example:

由于CLLocationCoordinate2D是一个结构体,您可以将两个mkannotation的坐标@properties与=进行比较。例子:

MKAnnotation *a1;
MKAnnotation *a2;

if(a1.coordinate == a2.coordinate) {
  //coordinates equal
}

With the caveat: you care comparing floating point values in the CLLocationCoordinate2D (the latitude and longitude fields of CLLocationCoordinate2D are of type CLLocation which is typdefed as double). As always, comparing two floating point values for equality is fraught with subtlety. You may want to do a more involved comparison of the latitude and longitude values independently (e.g. checking wither their absolute difference is within some small range). See Numerical Recipes for more info on this issue.

需要注意的是:您需要比较CLLocationCoordinate2D中的浮点值(CLLocationCoordinate2D的纬度和经度字段是CLLocation类型的,类型为double)。与往常一样,将两个浮点值与等式进行比较充满了微妙之处。您可能希望独立地对纬度和经度值进行更复杂的比较(例如,检查它们的绝对值是否在某个小范围内)。有关这个问题的更多信息,请参见数字食谱。

If you want to compare all properties, something like

如果你想比较所有的属性,比如

(a1.coordinate == a2.coordinate) && [a1.title isEqualToString:a2.title] && [a1.subtitle isEqualToString:a2.subtitle]

(again with the save caveat) would do the trick.

(再一次使用“保存警告”)将会起作用。

#1


2  

Since CLLocationCoordinate2D is a struct, you can compare the coordinate @properties of two MKAnnotations with ==. Example:

由于CLLocationCoordinate2D是一个结构体,您可以将两个mkannotation的坐标@properties与=进行比较。例子:

MKAnnotation *a1;
MKAnnotation *a2;

if(a1.coordinate == a2.coordinate) {
  //coordinates equal
}

With the caveat: you care comparing floating point values in the CLLocationCoordinate2D (the latitude and longitude fields of CLLocationCoordinate2D are of type CLLocation which is typdefed as double). As always, comparing two floating point values for equality is fraught with subtlety. You may want to do a more involved comparison of the latitude and longitude values independently (e.g. checking wither their absolute difference is within some small range). See Numerical Recipes for more info on this issue.

需要注意的是:您需要比较CLLocationCoordinate2D中的浮点值(CLLocationCoordinate2D的纬度和经度字段是CLLocation类型的,类型为double)。与往常一样,将两个浮点值与等式进行比较充满了微妙之处。您可能希望独立地对纬度和经度值进行更复杂的比较(例如,检查它们的绝对值是否在某个小范围内)。有关这个问题的更多信息,请参见数字食谱。

If you want to compare all properties, something like

如果你想比较所有的属性,比如

(a1.coordinate == a2.coordinate) && [a1.title isEqualToString:a2.title] && [a1.subtitle isEqualToString:a2.subtitle]

(again with the save caveat) would do the trick.

(再一次使用“保存警告”)将会起作用。