ObjC protocol Implementation in Swift

前端 未结 1 1432
长情又很酷
长情又很酷 2020-12-11 04:57

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:

相关标签:
1条回答
  • 2020-12-11 05:42

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

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

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

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

    (with lowercase "v" in value:), but none of

    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:

    class ViewController: UIViewController, MyProtocol
    {
        @objc(getAxisLabel:Value:)
        func getAxisLabel(axis: AnyObject!, value: CGFloat) -> String! {
            return ""
        }
    }
    
    0 讨论(0)
提交回复
热议问题