Swift:候选人的类型不匹配

标签 swift

我尝试在我的 swift 实现中使用以下委托(delegate)(见下文),但失败了 “不符合协议(protocol)......”和错误消息,如“候选人具有不匹配的类型(PieChartView) -> Int” 有什么想法吗?

@protocol PieChartViewDataSource <NSObject>

@required
- (int)numberOfSlicesInPieChartView:(PieChartView *)pieChartView;
- (double)pieChartView:(PieChartView *)pieChartView valueForSliceAtIndex:(NSUInteger)index;
- (UIColor *)pieChartView:(PieChartView *)pieChartView colorForSliceAtIndex:(NSUInteger)index;

失败的 Swift 实现

func numberOfSlicesInPieChartView(pieChartView: PieChartView) -> Int {
    return 3
}

func pieChartView(pieChartView: PieChartView, colorForSliceAtIndex index: Int) -> UIColor? {
    return UIColor(rgba: "#FF0000")
}

func pieChartView(pieChartView: PieChartView, valueForSliceAtIndex index: Int) -> Double {
    return 100/3;
}

最佳答案

func numberOfSlicesInPieChartView(pieChartView: PieChartView) -> CInt {
    return 32
}
func pieChartView(pieChartView: PieChartView, colorForSliceAtIndex index: UInt) -> UIColor {
    return UIColor(red: 1.0, green: 0, blue: 0, alpha: 1)
}

func pieChartView(pieChartView: PieChartView, valueForSliceAtIndex index: UInt) -> Double {
    return 100/3;
}

使用 -> CInt 而不是 Int 因为 Swift Int 无法转换为 C int。与 Double 相同 -> CDouble

使用 index: UInt 而不是 index: Int,因为 NSUIntegerunsigned

关于Swift:候选人的类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26419930/

相关文章:

ios - 从 URLSession Completion Handler 函数返回字符串 Swift 3

swift - 在 Xcode 6 中工作的代码不适用于 Xcode 7

swift - 如何快速将字符串转换为 Unicode?

ios - 如何在 PNG 的 Swift 中在保存到 .WriteToFile 的图像顶部插入标签

ios - 将视点转换为 MKMapView 坐标

swift - Swift 中的 "@exported"属性是什么

ios - 将数据从 VC 发送到单元,无需转换

ios - 使用 CoreLocation 提高点击按钮时获取最近地址的准确度

ios - TableView Cell 内的多个 View

arrays - Swift 数组中的 CGFloat