6

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:

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

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

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

In bridging header:

#import "StupidProtocol.h"

And then back in Swift file:

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.

I strongly suspect that the problem is with argument "value" and it's function parameter "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. 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

5
  • Obj c is uppercase V and swift is lowercase v. Make them both lowercase. It should be lowercase in both. Commented Jun 29, 2015 at 7:13
  • try writing the swift method with external param name "Value" and internal param name of "value" like: "Value value: CGFloat" Commented Jun 29, 2015 at 7:17
  • That might fix the problem but you shouldn't have the uppercase V in the first place. Drop it and it will work. Commented Jun 29, 2015 at 7:18
  • Please note that this method declaration is not mine - I totally agree it's declared wrong. But I need to use the library that has this declaration in Swift. If you could help me somehow with this it'll be great. Commented Jun 29, 2015 at 7:22
  • Then @hola fix of Value value: CGfloat should work Commented Jun 29, 2015 at 7:26

1 Answer 1

9

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 ""
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

You are a life saver! I knew it should be something with @objc keyword as a workaround but didn't know how to exactly use it. Great to know for future references also!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.