Search code examples
objective-cswiftcocoapodsios-frameworksswift-extensions

Swift extension in Cocoapods Framework causing Unrecognised Selector sent to Class


I have a Swift framework which is managed via Cocoapods and contains an extension like so:

public extension UIImage {
    public static func maskedImageWithColor( color: UIColor, forImageNamed image: UIImage) {
        // Implementation
    }
}

When I write it in Objective C in my main application I get no compiler errors or warnings.

// No problems here!
[UIImage maskedImageWithColor:UIColor.blackColor() forImageNamed:@"myImage"]; 

When I run the app, however, it explodes in a mess of 'Unrecognised Selector' errors.

The weird thing is, when the framework is contained within the project and added directly to the relevant parts of build phases (i.e. not managed by Cocoapods), it all works as expected.

Other aspects of the framework—classes, enums, etc.—all work fine too, it just seems to be extensions and only when they're in an framework managed by Cocoapods.

Any ideas on what I might be missing here?


Solution

  • This is because the linker is not loading category methods form static library by default. If you are adding category methods(extension in swift) with static library, add -ObjC to other linker flags in your targets build settings.

    Read more here