0

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.

David Nedrow
  • 1,098
  • 1
  • 9
  • 26
  • 1
    You test bundle is not compiling in `MKPolygon+PointInPolygon.m` file. Check the list of targets of that file in Xcode, make sure the test target is included. – hamstergene Mar 25 '16 at 21:07
  • That was it. I had not added the implementation file for the protocol to the test target. – David Nedrow Mar 25 '16 at 22:12
  • Also, you seem to have confused a[ category](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html) with a [protocol](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html#//apple_ref/doc/uid/TP40011210-CH11-SW1). They are not the same! A category extends an existing class. A protocol is an interface that a class (often one you're writing) must implement to perform a specific task or set of tasks. – user1118321 Mar 26 '16 at 05:03

1 Answers1

0

You should not add production code to your test target.

Instead, declare -ObjC in your Other Linker Flags so that it picks up category method implementations. For more information, see Objective-C categories in static library

Test targets, if set up correctly, can access everything in production code.

Community
  • 1
  • 1
Jon Reid
  • 20,545
  • 2
  • 64
  • 95