Reputation: 459
I have a swift protocol having following delegate method
@objc public protocol CuteDelegate: NSObjectProtocol {
@objc func myCuteFunc(name: NSString)
}
I have declared delegate object in swift as well
weak var delegate : CuteDelegate?
In my objective C controller where I am implementing the above delegate method is as follows
-(void)myCuteFunc:(NSString* )name{
}
But while calling the method in swift controller
self.delegate?.myCuteFunc(name: str as NSString)
I get unrecognized selector sent to instance
Any clue what's the issue
Upvotes: 0
Views: 514
Reputation: 154731
You need to account for the first arguments's name:
Either:
-(void)myCuteFuncWithName:(NSString* )name
Or:
@objc func myCuteFunc(_ name: NSString)
and call it with self.delegate?.myCuteFunc(str)
This is just an artifact of the way Objective-C function names work vs. the way Swift names its arguments. Objective-C has no way of naming the first argument (which is usually described by the function name), so if Swift has a label for the first argument, the convention used is to add With
plus the argument name (with the argument name capitalized) to the function name. By adding _
, you make the first argument unnamed and that translates better to the Objective-C naming convention.
Upvotes: 3