Search code examples
swiftasync-awaitconcurrency

Global static var provided by third party not concurrency safe


I have the following code declared at the top of my AppDelegate file, in the global scope.

let commandLineArguments = CommandLine.arguments

Xcode displays the warning Reference to static property 'arguments' is not concurrency-safe because it involves shared mutable state. How can I address this warning?


Solution

  • If a library or even one type from it that you want to use doesn’t support strict concurrency checking yet, you can add @preconcurrency before import to tell the compiler that you are importing a library that hasn’t been updated for Sendable checking yet:

    @preconcurrency import Foundation
    

    Or, specifically import the enum itself:

    @preconcurrency import enum Foundation.CommandLine
    

    When the type or the library gets updated for the Sendable checking, the compiler will tell you that @preconcurrency before your import is not needed anymore, and you can safely delete it.