本文介绍了与Swift集成Objective C协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,我需要一些帮助来进行整合一个组件AKPickerView(用Objective C编写).组件的链接: https://github.com/Akkyie/AKPickerView

I am new in swift and i need some help to integratea component AKPickerView (written in Objective C).link of component: https://github.com/Akkyie/AKPickerView

但是我没有成功,因为有一个协议: AKPickerViewDelegate 在Swift中不起作用:

But i have no success, because there is a protocol: AKPickerViewDelegate that don't work in Swift:

@class AKPickerView;

@protocol AKPickerViewDelegate <NSObject>
@optional
- (NSString *)pickerView:(AKPickerView *)pickerView titleForItem:(NSInteger)item;
- (NSUInteger)numberOfItemsInPickerView:(AKPickerView *)pickerView;
- (void)pickerView:(AKPickerView *)pickerView didSelectItem:(NSInteger)item;
@end

在Swift中,我编写了以下代码:

In Swift i wrote this code:

class ViewController: UIViewController, AKPickerViewDelegate {

var pickerView : AKPickerView = AKPickerView()
var titles     : NSArray = ["Janeiro","Fevereiro","Março","Abril","Maio"]

...

我暗含了这种方法:

func numberOfItemsInPickerView(pickerView : AKPickerView!) -> Int{

    return self.titles.count
}


func pickerView(pickerView: AKPickerView, titleForItem item : NSInteger) -> NSString {

    return self.titles[item] as NSString
}

但是编译器显示此错误:

But the compiler show this error:

Type 'ViewController' does not conform to protocol 'AKPickerViewDelegate'

感谢一些帮助谢谢!

推荐答案

正如我在链接AKPickerViewDelegate中看到的那样,它具有必需的方法.

As i have seen in link AKPickerViewDelegate has required method.

@protocol AKPickerViewDelegate <NSObject>
@required
- (NSUInteger)numberOfItemsInPickerView:(AKPickerView *)pickerView;
- (NSString *)pickerView:(AKPickerView *)pickerView titleForItem:(NSInteger)item;
@optional
- (void)pickerView:(AKPickerView *)pickerView didSelectItem:(NSInteger)item;
@end

因此,您需要快速实现它们.按如下所示实施这些方法,它将消除错误

So in swift you need to implement them.Implement these methods as below and it will remove the error

func numberOfItemsInPickerView(pickerView: AKPickerView!) -> UInt {

    return self.titles.count
}

func pickerView(pickerView: AKPickerView!, titleForItem item: Int) -> String! {

   return self.titles[item]
}

注意,您还需要将它们添加到桥接标题"中.只需在Swifter-Bridging-Header.h

Note you also need to add them in Bridging Header.Just write this #import "AKPickerView.h" in Swifter-Bridging-Header.h

这篇关于与Swift集成Objective C协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 03:51