Search code examples
iosobjective-cswiftmwphotobrowser

Objective-C protocol in Swift - shows does not conform error


I am trying use an Objective-C library (MWPhotoBrowser) in my Swift app. My Swift class conforms to MWPhotoBrowserDelegate protocol by implementing required methods. However, I keep getting the following error:

"Type 'PhotoLibrary' does not conform to protocol 'MWPhotoBrowserDelegate'"

Cocoa protocols seem to work fine. Has anyone encountered this issue before?

Here is the sample code:

class PhotoLibrary: UIImageView, MWPhotoBrowserDelegate {

    init() {
        super.init(frame: CGRectZero)
    }

    func numberOfPhotosInPhotoBrowser(photoBrowser: MWPhotoBrowser!) -> Int {
        return 0
    }

    func photoBrowser(photoBrowser: MWPhotoBrowser!, photoAtIndex index: Int) -> MWPhoto! {
        return nil
    }
}

Protocol definition is as follows:

@protocol MWPhotoBrowserDelegate <NSObject>

- (NSInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser;
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSInteger)index;

@optional

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index;
- (MWCaptionView *)photoBrowser:(MWPhotoBrowser *)photoBrowser captionViewForPhotoAtIndex:(NSUInteger)index;
- (NSString *)photoBrowser:(MWPhotoBrowser *)photoBrowser titleForPhotoAtIndex:(NSUInteger)index;
- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser didDisplayPhotoAtIndex:(NSUInteger)index;
- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser actionButtonPressedForPhotoAtIndex:(NSUInteger)index;
- (BOOL)photoBrowser:(MWPhotoBrowser *)photoBrowser isPhotoSelectedAtIndex:(NSUInteger)index;
- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index selectedChanged:(BOOL)selected;
- (void)photoBrowserDidFinishModalPresentation:(MWPhotoBrowser *)photoBrowser;

@end

Solution

  • Here's what I figured out:

    Swift has issues with id data type. I had to modify MWPhotoBrowser code to replace all instances of id to (MWPhoto *). I think MWPhoto * should have been used in the first place.

    That fixes the issue of non-conformance.