Description
Previous ID | SR-9035 |
Radar | None |
Original Reporter | stephengroom (JIRA User) |
Type | Bug |
Status | Resolved |
Resolution | Done |
Environment
Apple Swift version 4.2 (swiftlang-1000.11.37.1 clang-1000.11.45.1)
Target: x86_64-apple-darwin17.7.0
Additional Detail from JIRA
Votes | 0 |
Component/s | Compiler |
Labels | Bug, StarterBug |
Assignee | @theblixguy |
Priority | Medium |
md5: a75bc65432adfa13920e0584ecf94a27
Issue Description:
If you define the following protocol in Swift it causes an error:
@objc protocol TestProtocol {
func throwingMethod() throws -> Unmanaged<CFArray>
}
Throwing method cannot be a member of an @objc protocol because it returns a value of type 'Unmanaged<CFArray>'; return 'Void' or a type that bridges to an Objective-C class
The error is wrong as Unmanaged<CFArray> does bridge to an Objective-C class.
eg. if you define the following in Swift
@objc protocol TestProtocolWhichBridges {
func method() -> Unmanaged<CFArray>
func optionalMethod() -> Unmanaged<CFArray>?
}
the following appears in Module-Swift.h (albeit albeit with a warning) and can be used
@protocol TestProtocolWhichBridges
- (CFArrayRef _Nonnull __unsafe_unretained)method SWIFT_WARN_UNUSED_RESULT;
- (CFArrayRef _Nullable __unsafe_unretained)optionalMethod SWIFT_WARN_UNUSED_RESULT;
@end
//'objc_ownership' only applies to Objective-C object or block pointer types; type here is 'CFArrayRef' (aka 'const struct __CFArray *')
If you define a protocol in Objective-C which matches the signature of TestProtocol and view the file's generated interface it looks as you'd expect:
@protocol TestProtocol
- (CFArrayRef)throwingMethodWithError:(NSError **)error;
@end
It is imported into Swift as
public protocol TestProtocol {
public func throwingMethod() throws -> Unmanaged<CFArray>
}
but the protocol can't be implemented because of the error:
Throwing method cannot be an implementation of an @objc requirement because it returns a value of type 'Unmanaged<CFArray>'; return 'Void' or a type that bridges to an Objective-C class