Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 388
user3154427 Avatar asked Jul 27 '14 19:07

user3154427


People also ask

Can Objective-C class conform to Swift protocol?

In order to use Swift code inside Objective-C one must scrifice some Swift features and write a wrapper around original Swift code that won't use non-compatible features (like structs, generics, enum associated values, protocol extensions etc.). All wrapper classes must inherit NSObject .

How do you conform to a protocol in Objective-C?

Objective-C uses angle brackets to indicate conformance to a protocol. This example declares a weak property for a generic object pointer that conforms to the XYZPieChartViewDataSource protocol.

How do you call Swift protocol in Objective-C?

In Objective-C, protocols are declared with the “@protocol” keyword. Below is an example of declaring a protocol containing one required method. In Swift, the syntax is a little different but the idea is the same. In Objective-C, you add the protocol name in angle brackets beside the class interface declaration.

How do you inherit a Swift class in Objective-C?

Unfortunately, it's not possible to subclass a Swift class in Objective-C. Straight from the docs: You cannot subclass a Swift class in Objective-C. See Apple's guide on interoperability for more details on what you can and cannot access with Objective-C.


1 Answers

I got MWPhotoBrowser working in my Swift app with the following code without needing to change the MWPhotoBrowser codebase.

func showFullScreenImage(image:UIImage) {
    let photo:MWPhoto = MWPhoto(image:image)

    self.photos = [photo]

    let browser:MWPhotoBrowser = MWPhotoBrowser(delegate: self)

    browser.displayActionButton = true
    browser.displayNavArrows = false
    browser.displaySelectionButtons = false
    browser.zoomPhotosToFill = true
    browser.alwaysShowControls = false
    browser.enableGrid = false
    browser.startOnGrid = false
    browser.enableSwipeToDismiss = true

    browser.setCurrentPhotoIndex(0)

    self.navigationController?.pushViewController(browser, animated: true)
}

func numberOfPhotosInPhotoBrowser(photoBrowser: MWPhotoBrowser!) -> UInt {
    return UInt(self.photos.count)
}

func photoBrowser(photoBrowser: MWPhotoBrowser!, photoAtIndex index: UInt) -> MWPhotoProtocol! {
    if Int(index) < self.photos.count {
        return photos.objectAtIndex(Int(index)) as! MWPhoto
    }
    return nil
}

func photoBrowserDidFinishModalPresentation(photoBrowser:MWPhotoBrowser) {
    self.dismissViewControllerAnimated(true, completion:nil)
}

I set photos as a class var, e.g. private var photos = [], and added the following imports in my app's Bridging-Header.h file:

#import <MWPhotoBrowser/MWPhoto.h>
#import <MWPhotoBrowser/MWPhotoBrowser.h>

I got most of the above code from https://github.com/mwaterfall/MWPhotoBrowser/issues/325#issuecomment-64781477.

like image 179
Chico Avatar answered Oct 28 '22 18:10

Chico