I have a Swift project with a Bridging-Header to the following Objective-C method:
- (NSData* _Nullable)get_data_pointer_without_error; // header
- (NSData*)get_data_pointer_without_error {
return NULL;
}
The Swift signature (as expected) is func get_data_pointer_without_error() -> Data?
. When I call this from Swift it successfully returns a nil Data?
. But here is the same method which also can throw an error:
- (NSData* _Nullable)get_data_pointer_with_error:(NSError* _Nullable* _Nullable)error; // header
- (NSData*)get_data_pointer_with_error:(NSError* _Nullable* _Nullable)error {
return NULL;
}
When I call this from Swift it crashes with the error shown below. Xcode shows that the Swift signature is func get_data_pointer_with_error() throws -> Data
. It adds throws
as expected, but instead of Data?
it returns Data
. (No surprise that the return of NULL from Objective-C makes it crash.)
My question: How to return a nullable NSData*
from an Objective-C method which also throws?
Sample project: https://github.com/jefft0/swift-objc-data/blob/main/swift-objc-data/main.swift
Output:
get_data_pointer_without_error returned
Fatal error: Error raised at top level: Foundation._GenericObjCError.nilError: file /AppleInternal/BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.8.25.8/swift/stdlib/public/core/ErrorType.swift, line 200