简体   繁体   English

Swift中的ObjC协议实现

[英]ObjC protocol Implementation in Swift

Ok here is the big problem. 好的,这是个大问题。 I had a library written in ObjC( this ). 我有一个用ObjC( this )编写的库。 There we had a defined protocol. 在那里,我们有一个已定义的协议。 When I tried to use it in swift file I get constantly: 当我尝试在快速文件中使用它时,我经常得到:

Type "XXX" does not conform to protocol "XXX" 类型“ XXX”不符合协议“ XXX”

To simplify things I made up a test project - it should be created as Swift project. 为简化起见,我组成了一个测试项目-应该将其创建为Swift项目。

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): 然后使用以下协议在内部创建ObjC头文件(我称其为StupidProtocol.h)(请注意,每个名称和值都应与给定的值完全匹配,包括大写/小写):

@protocol MyProtocol <NSObject>

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

@end

In bridging header: 在桥接头中:

#import "StupidProtocol.h"

And then back in Swift file: 然后返回Swift文件:

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

And baam we got this error again even though auto-complete finishes the getAxisLabel function for me. 而且,即使自动完成为我完成了getAxisLabel函数,我们还是再次遇到此错误。

I strongly suspect that the problem is with argument "value" and it's function parameter "Value". 我强烈怀疑问题出在参数“ value”及其功能参数“ Value”。

Any help will be highly appreciated. 任何帮助将不胜感激。

EDIT 编辑

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. 我需要一种在Swift中使用它而不更改其原始声明的方法。 My example is only to simplify my problem. 我的例子只是为了简化我的问题。

WHAT I TRIED 我尝试过的

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 ""
}

'type does not conform to protocol' '类型不符合协议'

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

SOLUTION

See accepted answer 查看已接受的答案

I don't know if this is a bug or not. 我不知道这是否是错误。 The Objective-C protocol method Objective-C协议方法

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

(with uppercase "V" in Value: ) is mapped to Swift as (在Value:使用大写的“ V”)映射为Swift

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

(with lowercase "v" in value: ), but none of value:小写字母“ v” :),但没有

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

is accepted by the compiler to satisfy the protocol requirement. 被编译器接受以满足协议要求。

As a workaround, you can annotate the method with an explicit Objective-C selector name: 解决方法是,可以使用显式的Objective-C选择器名称注释该方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM