I'm trying to conform to an Objective-C delegate method on an Objective-C class via a swift extension on said class. The problem is I am given no autocomplete options when defining the body for this function in the swift extension below. I had to manually translate it from Objective-C and it still doesn't work.. I know the delegate is set up correctly as when I provide the delegate function body in the ObjectiveCConformingClass directly it works fine (and autocompletes when I write the function).
I have a swift class like this (what I need to fix I imagine):
extension ObjectiveCConformingClass {
func delegateFunction(with index: Int, viewName: String, action: String, dictionary: [String : Any]) {
//Never gets called.
}
}
Extending a class like this:
Class that conforms to the delegate:
@interface ObjectiveCConformingClass : SuperClass <ObjectiveCDelegate>
//Whatever
@end
Delegate:
@protocol ObjectiveCDelegate <NSObject>
@optional
- (void)delegateFunction:(NSInteger)index
view:(nonnull NSString *)view
action:(nonnull NSString *)action
dictionary:(nonnull NSDictionary<NSString *, id> *)dictionary;
@end
So to summarise: I need to conform to this delegate in a swift extension of the class, not the actual class. It works fine if I do it directly in the objective-C class. Any ideas why it is not working? Or if this is even possible?
Here are some reasonably similar questions that ask for different things so did not help me. (i.e. threads that this is not a duplicate of)
Take your time when reading..
Think of delegates as
id<ProtocolName>"pointer to another object" that conforms to a protocol.Usually your
@interface ClassName : NSObjectthat wants to make use of the delegate should have a property to keep it around or to set itnil(nullable) which implies why you want it weak.and the class (object) that will become your delegate must conform to this protocol, so you have to declare this in its interface as you did. In full beauty looks like..
Because the protocol above has optional method declaration it will not throw warnings when you did not implement it. To avoid running in trouble when working with the delegate from within your class with
ClassName(object) you will want to check if the delegate property is notniland can respond to the desired method name.so far its working in objective-c
now in swift you can make use of the delegate even in an extension
let's check if swift extensions works properly when subclassed.
lets test
Hope this is not too confusing but you see who is calling who and what is invoked.