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
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, eg 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 .
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.
xCode 7.1 - ran into some issues but got it to work.
Imported through Cocoa Pods.
Bridging Header add:
#import <MWPhotoBrowser/MWPhoto.h>
#import <MWPhotoBrowser/MWPhotoBrowser.h>
Build Settings -> Header Search Paths add "Pods" set to recursive (fixes bridging header error)
Build Phases -> Link Binary with Libraries add: libMWPhotoBroswer, MediaPlayer.framework, libSDWebImage, libMBProgressHUD and libDACIrcularProgress (fixes 'symbol(s) not found for architecture x86_64' errors)
Build Settings -> Other Linker Flags add '-ObjC' (fixes errors with SDWebImage framework)
Hope that helps someone.
I got it working with swift 3.0, I has followed below steps:
(1) Edit MWPhotoBrowser.h
file and find/replace " id <MWPhoto>
" to " MWPhoto *
"
(2) Implement required function as:
public func numberOfPhotos(in photoBrowser: MWPhotoBrowser!) -> UInt {
return UInt(arrPhotos.count)
}
public func photoBrowser(_ photoBrowser: MWPhotoBrowser!, photoAt index: UInt) -> MWPhoto! {
return arrPhotos[Int(index)] as MWPhoto
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.