[英]Objective-C protocol in Swift - shows does not conform error
我正在尝试在我的Swift应用程序中使用Objective-C库(MWPhotoBrowser)。 我的Swift类通过实现所需的方法符合MWPhotoBrowserDelegate协议。 但是,我不断收到以下错误:
“Type'PhotoLibrary'不符合协议'MWPhotoBrowserDelegate'”
Cocoa协议似乎工作正常。 有没有人遇到过这个问题?
以下是示例代码:
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 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
我使用以下代码在我的Swift应用程序中使用MWPhotoBrowser,而无需更改MWPhotoBrowser代码库。
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)
}
我将照片设置为类var,例如private var photos = []
,并在我的应用程序的Bridging-Header.h文件中添加了以下导入:
#import <MWPhotoBrowser/MWPhoto.h>
#import <MWPhotoBrowser/MWPhotoBrowser.h>
我从https://github.com/mwaterfall/MWPhotoBrowser/issues/325#issuecomment-64781477获得了上述大部分代码。
这是我想出的:
Swift存在id数据类型的问题。 我不得不修改MWPhotoBrowser代码以将所有id实例替换为(MWPhoto *)。 我认为MWPhoto *应该首先使用。
这解决了不合规问题。
xCode 7.1 - 遇到了一些问题,但让它发挥作用。
通过可可豆荚进口。
桥接标题添加:
#import <MWPhotoBrowser/MWPhoto.h>
#import <MWPhotoBrowser/MWPhotoBrowser.h>
构建设置 - >标题搜索路径添加“Pods”设置为递归(修复桥接标题错误)
构建阶段 - >链接二进制文件库添加:libMWPhotoBroswer,MediaPlayer.framework,libSDWebImage,libMBProgressHUD和libDACIrcularProgress(修复了未找到架构x86_64错误的符号)
构建设置 - >其他链接器标志添加'-ObjC'(修复SDWebImage框架的错误)
希望能帮助别人。
我使用swift 3.0,我遵循以下步骤:
(1)编辑MWPhotoBrowser.h
文件并查找/替换“ id <MWPhoto>
”到“ MWPhoto *
”
(2)实现所需的功能:
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.