Objective-C协议在Swift -显示不符合错误



我正在尝试在我的Swift应用程序中使用Objective-C库(MWPhotoBrowser)。我的Swift类通过实现所需的方法符合MWPhotoBrowserDelegate协议。然而,我一直得到以下错误:

"类型'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

我得到了MWPhotoBrowser工作在我的Swift应用程序与以下代码不需要改变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 = [],并在我的应用程序的bridge - 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 -遇到了一些问题,但使其工作

  1. 通过Cocoa Pods导入

  2. 桥接头add:

    #import <MWPhotoBrowser/MWPhoto.h>

    #import <MWPhotoBrowser/MWPhotoBrowser.h>

  3. Build Settings -> Header Search Paths添加"Pods"设置为递归(修复桥接报头错误)

  4. 构建阶段->链接二进制库添加:libmwphotobrowser, MediaPlayer.framework, libSDWebImage, libMBProgressHUD和libDACIrcularProgress(修复了'symbol(s) not found for architecture x86_64'错误

  5. 构建设置->其他链接标志添加'- 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
}

最新更新