Swift中的ObjC协议实现 [英] ObjC protocol Implementation in Swift

查看:128
本文介绍了Swift中的ObjC协议实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是一个大问题。我有一个用ObjC编写的库(这个)。我们有一个定义的协议。当我试图在swift文件中使用它时,我经常得到:

Ok here is the big problem. I had a library written in ObjC(this). There we had a defined protocol. When I tried to use it in swift file I get constantly:


类型XXX不符合协议XXX

Type "XXX" does not conform to protocol "XXX"

为了简化我构建测试项目的事情 - 它应该被创建为Swift项目。

To simplify things I made up a test project - it should be created as Swift project.

然后使用以下协议创建ObjC头文件(我称之为StupidProtocol.h)(请注意每个名称和值与给定的完全匹配,包括大写/小写):

Then create ObjC header file(I called it StupidProtocol.h) with following protocol inside(please note each name and value to exactly match the given including uppercase/lowercase):

@protocol MyProtocol <NSObject>

- (NSString *)getAxisLabel:(id)axis Value:(CGFloat)value;

@end

在桥接标题中:

#import "StupidProtocol.h"

然后回到Swift文件中:

And then back in Swift file:

class ViewController: UIViewController, MyProtocol
{
    func getAxisLabel(axis: AnyObject!, value: CGFloat) -> String! {
        return ""
    }
}

和baam我们即使auto-complete为我完成了getAxisLabel函数,也会再次出现此错误。

And baam we got this error again even though auto-complete finishes the getAxisLabel function for me.

我强烈怀疑问题是参数value和它的函数参数Value。

I strongly suspect that the problem is with argument "value" and it's function parameter "Value".

任何帮助将非常感谢。

编辑

请注意这个图书馆在技术上并不是我的,所以我无法改变它。我需要一种方法在Swift中使用它而不改变它的原始声明。我的例子只是为了简化我的问题。

Please note that this library is not technically mine, so I cannot change it. I need a way to use it in Swift without changing it's original declaration. My example is only to simplify my problem.

我做了什么

我试过以下方案但没有成功:

I have tried following scenarios with no success:

'具有与协议所需的名称不同的参数'错误

'has different arguments name from those required by protocol' error

func getAxisLabel(axis: AnyObject!, Value value: CGFloat) -> String! {
    return ""
}

'具有与所需参数不同的参数名称通过协议'错误

'has different arguments name from those required by protocol' error

func getAxisLabel(axis: AnyObject!, Value: CGFloat) -> String! {
    return ""
}

'类型不符合协议'

func getAxisLabel(axis: AnyObject!, Value: CGFloat) -> NSString! {
    return ""
}

解决方案

请参阅接受的答案

推荐答案

我不知道这是不是一个bug。 Objective-C协议方法

I don't know if this is a bug or not. The Objective-C protocol method

- (NSString *)getAxisLabel:(id)axis Value:(CGFloat)value;

中的大写V值:)映射到Swift为

(with uppercase "V" in Value:) is mapped to Swift as

func getAxisLabel(axis: AnyObject!, value: CGFloat) -> String!

中的小写v值:),但没有

func getAxisLabel(axis: AnyObject!, value: CGFloat) -> String! { }
func getAxisLabel(axis: AnyObject!, Value: CGFloat) -> String! { }

以满足协议要求。

作为一种变通方法,您可以使用显式的
Objective-C选择器名称来注释该方法:

As a workaround, you can annotate the method with an explicit Objective-C selector name:

class ViewController: UIViewController, MyProtocol
{
    @objc(getAxisLabel:Value:)
    func getAxisLabel(axis: AnyObject!, value: CGFloat) -> String! {
        return ""
    }
}

这篇关于Swift中的ObjC协议实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆