vendredi 25 mars 2016

Objective-C protocol fails selector in XCTest, but not in app

I have an odd issue where a call to a protocol fails with an unrecognized selector, but the same bit of code works fine in the app itself. Anyone out there know why this is happening? Here is what I'm working with.

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+PointInPolygonTests.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);
}

The call to [polygon containsCoordinate:outsidePolygonLocation] causes the unrecognized selector error.

If I copy that above test method code into the application proper --- sans the XCTAssertion --- it works fine.

I'm at a loss as to what is happening. I've had some other people look at this, and they don't see anything wrong either.

Aucun commentaire:

Enregistrer un commentaire