Can NSXPCInterface work with non-void return type?

The official document says, "All messages must be 'void' return type." (ref)

However, in the file provider sample code of wwdc21, it uses it with non-void return type.
(Reference)

I wondered whether it can or can not be used with non-void return type?
Is the answer changed after swift support for async/await?

That's the relevant bit you are talking about from the sample code:

@objc public protocol FruitBasketServiceV1 {
    func versions(for url: URL) async throws -> ([NSData], NSData)
    func keep(versions: [NSNumber], baseVersion: NSData, for url: URL) async throws
}

which would match the older style:

func versions(for url: URL, execute: @escaping ([NSData], NSData) -> Void) throws

I'd say trust sample code in this case. Documentation in your ref link was obviously written before async/await surfaced and hasn't been updated.

1 Like

Is the answer changed after swift support for async/await?

Kind of.

This is just a consequence of how @objc async functions get imported into Objective-C.

Under the hood, the generated Objective C function has a void return type and has a completion handler, just like the docs would expect.

2 Likes