@objc attribute & Windows

Both @objc and @objcMembers attributes break compilation on Windows. I tried to wrap them in #if canImport(Darwin) but that breaks declaration. What is the preferred way to port existing sources what use these attributes on non-Darwin platforms?

1 Like

It is a bug. Those attributes are properly ignored on Linux, Android and web. Windows is supposed to as well. It does so in some contexts, but has been know to fail in others. If you have found such context, report it here: bugs.swift.org.

This is the proper way to use @objc in cross‐platform code:

@objc func doSomething() {}

In instances where that triggers a bug, this is how I work around it:


#if os(Windows)
  func doSomething() { _doSomething() }
#else
  @objc func doSomething() { _doSomething() }
#endif
private func _doSomething() {
  // ...
}

Unfortunately, In my case it is a type declaration.

I filled a bug report https://bugs.swift.org/browse/SR-14381

1 Like
Terms of Service

Privacy Policy

Cookie Policy