Description
Description
I have an Objective-C API that makes a network call, parses the resulting data as json, and returns it as a dictionary in a completion block. I get this warning (or error in Swift 6 mode) when I call it from Swift as an async method:
Non-sendable type '[AnyHashable : Any]?' returned by implicitly asynchronous call to nonisolated function cannot cross actor boundary
I've tried to annotate the function declaration using NS_SWIFT_SENDING
(from SE-0430), but it seems to have no effect.
Reproduction
Objective-C function:
//in class NetworkUtil:
+(void)fetchJsonFrom:(NSURL *_Nonnull)url completion:(void (^_Nonnull)(NSDictionary * _Nullable))completion;
{
[[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *parsed = nil;
if (data)
parsed = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if ([parsed isKindOfClass:[NSDictionary class]])
completion(parsed);
else
completion(nil);
}] resume];
}
Calling it from Swift:
func test() {
Task {
let json = await NetworkUtil.fetchJson(from: URL(string: "https://www.example.com")!)
}
}
Expected behavior
I should be able to annotate the Objective-C method somehow to tell Swift that the block parameter is safe to transfer across the actor boundary - what sending
is supposed to allow, as far as I understand.
Environment
% swiftc -version
swift-driver version: 1.115 Apple Swift version 6.0.2 (swiftlang-6.0.2.1.2 clang-1600.0.26.4)
This is Xcode 16.1, on macOS Sequoia 15.1.1.
Additional information
No response