Reputation: 4513
Is it a possible for an Objective-C
class to implement a Swift
protocol in the .h
so that other Swift
classes can reference the Objective-C
class by the Swift
protocol type?
Let MySwiftProtocol
be the Swift
protocol type, now assume I have this in .h
for my class:
@protocol MySwiftProtocol;
@interface MyObj : NSObject< MySwiftProtocol>
Then I'll get a warning: cannot find protocol definition for MySwiftProtocol
The warning is no good, so that doesn't work.
So let's assume I do this in the .m
and remove the references to the protocol in the .h
.
@interface MyObj () <MySwiftProtocol>
Then I end up not being able to cast objects of type MyObj
to type MySwiftProtocol
in Swift
classes.
So neither approach above works.
Please help!
Upvotes: 2
Views: 1099
Reputation: 4513
According to apple. Forward declarations of Swift classes and protocols can only be used as types for method and property declarations
. Further apple says, An Objective-C class can adopt a Swift protocol in its implementation (.m) file by importing the Xcode generated header for Swift code...
Basically you cannot mark an obj-c class as conforming to a swift protocol in the .h. So you must do it in the .m and then have the class pass itself to any object that is expecting a reference the objc object as the swift protocol type.
See sections Referencing a Swift Class or Protocol in an Objective-C Header
and Adopting a Swift Protocol in an Objective-C Implementation
at https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
Upvotes: 2