@available unavailable not working in a Swift Package

I have some common code I've placed in a Swift Package I want to use on any platform. I have a struct that depends on the Foundation Process class, that should only be made available on macOS. I'm new to cross platform issues, so I've read up and created my @available attributes, but I'm still getting errors when I try to compile for iOS. Surely with the attributes I've specified it should compile, no? What am I doing wrong?

23

@available doesn't prevent the compiler from trying to compile a declaration. You need #if os(macOS) for this. Then, in the #else branch, have a dummy definition for ProcessController with an @available(*, unavailable) attribute.

Note that @available(macOS 10.0, *) does not mean "available for macOS, unavailable otherwise." It means "available for macOS 10.0 and later, unconditionally available on all other platforms".

OK cool, so I better remove that, thanks. So when do the @available attributes influence the compiler? If I just have the source code in a Swift package and pull that package's code into an iOS app project, and try to compile that project, will I see compiler warnings in places where I've tried to use ProcessController from an iOS app codebase?

Trying to understand when these attributes actually have any influence is literally impossible to do just using the Swift Reference, because there simply isn't any documentation on how do use them, other than an explanation of the syntax.

Thanks for the explanation and advice. Can't say I'm thrilled about having to provide dummy implementations however. So these attributes really only influence the compiler once the definitions they are marking are compiled?

I believe so.

Terms of Service

Privacy Policy

Cookie Policy