79099575

Date: 2024-10-17 19:28:04
Score: 1
Natty:
Report link

Just make your files as an actor like below I do:

actor SingletonWithStatic {
  private init() { }
  private var output = ""
  static let shared = SingletonWithStatic()

  func prepare(outputLine line: String) {
    output += line
    output += "\n"
  }

  func flushOutput() {
    print(output)
    output = ""
  }
}

Or you can silent your warning by making your static variable unsafe, but make sure you use that static variable in thread safe manner.

class SingletonWithStatic {
  private init() { }
  private var output = ""
  nonisolated(unsafe) static let shared = SingletonWithStatic()

  func prepare(outputLine line: String) {
    output += line
    output += "\n"
  }

  func flushOutput() {
    print(output)
    output = ""
  }
}

Or mark your class or struct as @MainActor

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @MainActor
  • Low reputation (1):
Posted by: Akash Gupta