如何测试方法的协议? [英] How to test a protocol for a method?

查看:95
本文介绍了如何测试方法的协议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 4之前,我曾经为添加到地图视图的每个 MKAnnotationView 添加一个观察者,监听它的选择方法,所以我知道用户已经点击一个pin。

Before iOS 4, I used to add a observer to each MKAnnotationView added to the map view, listening for it's selected method, so I know when the user has tapped on a pin.

这对iOS 4.2的工作正常。我注意到在发布注释注释视图实际上被重复使用它以某种方式混淆了观察者。

This worked fine up to iOS 4.2. I've noticed on the release noted annotation views are actually being reused and it somehow messes up with the observers.

所以我想我可以使用 -mapview:didSelectAnnotationView:方法从 MKMapViewDelegate 为我的需要,但只有添加到iOS 4.0 SDK。

So I figure I can use the -mapview:didSelectAnnotationView: method from MKMapViewDelegate for my needs, but that has only been added to iOS 4.0 SDK.

因此,为了保持兼容性,我想在我的代理上实现这个方法,并有条件地检查 MKMapViewDelegate 协议,如果它不存在,我将添加我的观察者到注释视图。

So, to maintain compatibility, I'd like to implement this method on my delegate and conditionally check for the presence of this method on the MKMapViewDelegate protocol so that if it's not present, I will add my observer to the annotation view.

我如何做一个协议方法,类似的我们如何检查if a class is not nil?

How can I do this for a protocol method, similarly for how we check if a class is not nil?

UPDATE

正如Daniel Dickison指出的,我不能使用 responsesToSelector:,因为我的委托具有 -mapview:didSelectAnnotationView:实现4.0+设备。我需要的是检查该设备上的协议是否具有可选的 -mapview:didSelectAnnotationView:方法 if MKMapView

As Daniel Dickison pointed out, I can't use respondsToSelector:, because my delegate has -mapview:didSelectAnnotationView: implemented for 4.0+ devices. What I need is to check if the protocol on that device has the optional -mapview:didSelectAnnotationView: method OR if MKMapView will look for that method on it's delegate.

我最后对当前的iOS版本运行进行测试。如果它高于4.0, MKMapView 将寻找该方法并调用它。

I ended up doing a test for the current iOS version running. If it's higher than 4.0, MKMapView will look for that method and call it.

if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 4.0)
    [self setupObserver];

这解决了原来的问题,但仍然有趣的是检查方法的实际协议。

This solves the original problem, but it would still be interesting to check the actual protocol for the method somehow.

推荐答案

这是一个棘手的问题。所以如果我正确地理解你的问题,你想要在运行时找出地图视图是否发送 mapView:didSelectAnnotationView:消息给它的代理。但是你不能使用 conformsToProtocol: responsesToSelector:,因为你正在实现代理,

That's a tricky one. So if I'm understanding your question correctly, you want to find out, at runtime, whether the map view sends the mapView:didSelectAnnotationView: message to its delegate. But you can't use conformsToProtocol: or respondsToSelector: because you're implementing the delegate so obviously you're adopting the protocol and implementing the method.

我唯一能想到的是检查添加到 MKMapView mapRectThatFits:

The only thing I can think of is to check for some other method that was added to MKMapView (not the delegate) in iOS 4, like: mapRectThatFits:.

另一个可能性是请使用 Objective-C运行时库查询Protocol对象。但这可能是过度的,我也不认为它会工作,因为协议对象由编译器创建,当你构建你的应用程序,它可能会得到UIKit SDK定义 MKMapViewDelegate 协议对象,而不是编译的任何运行时。

Another possibility is to use the Objective-C runtime library to query the Protocol object. But this is probably overkill, and also I don't think it will work because the Protocol object is created by the compiler when you build your app, and it's possible you'll get the UIKit SDK-defined MKMapViewDelegate protocol object instead of whatever the runtime was compiled with.

这篇关于如何测试方法的协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆