gpt4 book ai didi

ios - Swift 中的 Objective-C 协议(protocol) - 显示不符合错误

转载 作者:搜寻专家 更新时间:2023-10-30 23:03:21 26 4
gpt4 key购买 nike

我正在尝试在我的 Swift 应用程序中使用 Objective-C 库 (MWPhotoBrowser)。我的 Swift 类通过实现所需的方法来符合 MWPhotoBrowserDelegate 协议(protocol)。但是,我不断收到以下错误:

“类型‘PhotoLibrary’不符合协议(protocol)‘MWPhotoBrowserDelegate’”

Cocoa 协议(protocol)似乎工作正常。以前有人遇到过这个问题吗?

示例代码如下:

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)定义如下:

@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)
}

我将照片设置为类变量,例如private var photos = [],并在我的应用程序的 Bridging-Header.h 文件中添加了以下导入:

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

我从 https://github.com/mwaterfall/MWPhotoBrowser/issues/325#issuecomment-64781477 得到了上面的大部分代码.

关于ios - Swift 中的 Objective-C 协议(protocol) - 显示不符合错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24984992/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com