I’m starting a project in Xcode, but its goal is to be cross-platform. For the moment I’m not trying to build it on any other OSs, but I want to avoid accidentally using Apple-specific APIs that will cause problems later on.
Unfortunately, Apple’s Foundation framework has a lot of stuff that’s not in Swift Foundation, e.g. InputStream/OutputStream\*. Is there anything I can do to avoid accidentally creating platform dependencies? It would be nice to opt into a compiler warning like InputStream is not available on all platforms, or to import some wrapper Foundation that only re-exports the cross-platform symbols.
At the very least, the Foundation API docs could indicate platform support, the way Kotlin’s do. But they’re hosted by Apple, which doesn’t really have any incentive to do that…
–Jens
\* BTW, is there any package that provides a lightweight stream API? I know about Swift-NIO but that seems way too big and complex for my needs.
The only real way, if you’re developing on macOS is to build on a different platform. So either CI or using dev containers in VSSCode/Docker or cross compiling. Unfortunately the cross-platform developer experience is pretty awful on macOS, when compared to other platforms. This is a known issue though and hopefully things will be improved.
For streaming APIs, what exactly are you trying to stream?
However, they're in full Foundation on non-Apple platforms, not in FoundationEssentials. Is that what you're thinking of? Or are there specific APIs on those types missing?
Files, and in memory (i.e. producing binary data by writing to a stream then getting the result as a Data.)
The API of InputStream/OutputStream is pretty awful and forces you to deal with unsafe buffers; it was clearly just a direct bridge to NSStream. I would think there’d be something cleaner by now.
I’m using BinaryParsing, which is quite nice, but of course it only reads from memory and it doesn’t have a counterpart for writing binary data.