Ios Objective-C协议在XCTest中的选择器失败,但在应用程序中未失败

Ios Objective-C协议在XCTest中的选择器失败,但在应用程序中未失败,ios,objective-c,unit-testing,polygon,xctest,Ios,Objective C,Unit Testing,Polygon,Xctest,我有一个奇怪的问题,对一个协议的调用由于无法识别的选择器而失败,但同样的代码在应用程序本身中也可以正常工作。有人知道为什么会这样吗?这就是我的工作 MKPolygon+PointInPolygon.h @import Foundation; @import MapKit; @interface MKPolygon (PointInPolygon) - (BOOL)containsCoordinate:(CLLocationCoordinate2D)coordinate; @end MKP

我有一个奇怪的问题,对一个协议的调用由于无法识别的选择器而失败,但同样的代码在应用程序本身中也可以正常工作。有人知道为什么会这样吗?这就是我的工作

MKPolygon+PointInPolygon.h

@import Foundation;
@import MapKit;

@interface MKPolygon (PointInPolygon)

- (BOOL)containsCoordinate:(CLLocationCoordinate2D)coordinate;

@end
MKPolygon+PointInPolygon.m

#import "MKPolygon+PointInPolygon.h"

@implementation MKPolygon (PointInPolygon)

- (BOOL)containsCoordinate:(CLLocationCoordinate2D)coordinate
{
    MKMapPoint mapPoint = MKMapPointForCoordinate(coordinate);
    MKPolygonRenderer *polygonView = [[MKPolygonRenderer alloc] initWithPolygon:self];
    CGPoint polygonViewPoint = [polygonView pointForMapPoint:mapPoint];
    BOOL returnResult = CGPathContainsPoint(polygonView.path, NULL, polygonViewPoint, NO);

    return returnResult;
}

@end
MKPolygon+pointinpolygon测试.m

#import <XCTest/XCTest.h>
#import "MKPolygon+PointInPolygon.h"

@interface MKPolygon_PointInPolygonTests : XCTestCase

@end

@implementation MKPolygon_PointInPolygonTests

- (void)testThatCoordinateIsNotInPolygon {
        //Canton
        CLLocationCoordinate2D outsidePolygonLocation = CLLocationCoordinate2DMake(40.798946, -81.378448);

        //Columbus Bounds
        CLLocationCoordinate2D nw = CLLocationCoordinate2DMake(40.0, -83.0);
        CLLocationCoordinate2D ne = CLLocationCoordinate2DMake(40.0, -82.0);
        CLLocationCoordinate2D se = CLLocationCoordinate2DMake(39.0, -82.0);
        CLLocationCoordinate2D sw = CLLocationCoordinate2DMake(39.0, -83.0);

        CLLocationCoordinate2D coords[] = {nw, ne, se, sw};

        MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coords count:4];

        BOOL result = [polygon  containsCoordinate:outsidePolygonLocation];

        XCTAssertFalse(result);
}
#导入
#导入“MKPolygon+PointInPolygon.h”
@接口MKPolygon_PointInPolygonTests:XCTestCase
@结束
@实现MKPolygon_pointinpolygon测试
-(void)坐标不在多边形中的测试{
//州
CLLocationCoordinate2D outsidePolygonLocation=CLLocationCoordinate2DMake(40.798946,-81.378448);
//哥伦布边界
CLLocationCoordinate2D nw=CLLocationCoordinate2DMake(40.0,-83.0);
CLLocationCoordinate2D ne=CLLocationCoordinate2DMake(40.0,-82.0);
CLLocationCoordinate2D se=CLLocationCoordinate2DMake(39.0,-82.0);
CLLocationCoordinate2D sw=CLLocationCoordinate2DMake(39.0,-83.0);
CLLocationCoordinate2D坐标[]={nw,ne,se,sw};
MKPolygon*polygon=[MKPolygon polygon-withcoordinates:coords count:4];
布尔结果=[多边形包含坐标:外部多边形定位];
xctasertfalse(结果);
}
调用[polygon containsCoordinate:outsidePolygonLocation]会导致无法识别的选择器错误

如果我将上面的测试方法代码复制到应用程序中(无XCTASSERION),它可以正常工作

我不知道发生了什么事。我已经让其他一些人看到了这一点,他们也没有发现任何错误。

您不应该向测试目标添加生产代码

相反,在其他链接器标志中声明
-ObjC
,以便它选择类别方法实现。有关详细信息,请参阅


如果设置正确,测试目标可以访问生产代码中的所有内容。

您的测试包未在
MKPolygon+PointInPolygon.m
文件中编译。在Xcode中检查该文件的目标列表,确保包含测试目标。就是这样。我没有将协议的实现文件添加到测试目标中。此外,您似乎将[category]()与[category]混淆了。他们不一样!类别扩展现有类。协议是类(通常是您正在编写的类)必须实现以执行特定任务或一组任务的接口。