I've read along here and here about conforming to Swift protocols in Objective-C headers, and I'm not quite getting the behaviour I want - I'm also looking for a better understanding of how this all works. Here's my scenario.
I have a protocol in Swift:
PersonalDetailsProtocol.swift
@objc
protocol PersonalDetailsProtocol {
func doSomeWork()
}
I then have an Objective-C class with a header and implementation file
RegistrationPersonalDetails.h
@protocol PersonalDetailsProtocol; // Forward declare my Swift protocol
@interface RegistrationPersonalDetails : NSObject <PersonalDetailsProtocol>
@end
RegistrationPersonalDetails.m
#import "RegistrationPersonalDetails.h"
@implementation RegistrationPersonalDetails
- (void)doSomeWork {
NSLog(@"Working...");
}
@end
At this point everything compiles, although there is a warning in the RegistrationPersonalDetails.h
file stating Cannot find protocol definition for 'PersonalDetailsProtocol'
. Other than that warning, the issue I'm facing is I can't publicly call the doSomeWork
method on an instance of RegistrationPersonalDetails
.
The call site in Swift would look something like:
let personalDetails = RegistrationPersonalDetails()
personalDetails.doSomeWork()
but I get an error stating:
Value of type 'RegistrationPersonalDetails' has no member 'doSomeWork'
I get that the method isn't public, because it's not declared in the header file. But I didn't think it should have to be as long as the protocol conformance is public i.e. declared in the header file.
Can anyone point me on the right path here and offer an explanation? Or is this even possible? I can obviously rewrite the protocol in ObjC, but I just always try to add new code as Swift.