Runtime error using example code for logging In XC12

When trying to add the example code from WWDC 2020 to my project I get

dyld: lazy symbol binding failed: Symbol not found: _$s2os6LoggerVMa
...
Expected in: /usr/lib/swift/libswiftos.dylib

Code Block import os
@available(OSX 10.16, *)
let logger = Logger(subsystem: "com.example.Fruta", category: "giftcards")
@available(OSX 10.16, *)
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    var window: NSWindow!
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        logger.log("logged Message")


Answered by Developer Tools Engineer in 615502022
I guess the app is running on macOS 10.15. What is happening here is that, since AppDelegate is annotated as NSApplicationMain, it is expected to be available on all deployment targets this app targets. In macOS 10.15, NSApplicationMain() will load the nib, which instantiates the AppDelegate and then runs the applicationDidFinishLaunching() method. Even though AppDelegate has 10.16 only code in it, nib loading will happily instantiate it regardless of the running OS version, which causes this error. Since AppDelegate is marked as only available on 10.16, the compiler doesn’t complain about the use of logger.log.

The solution to this problem is to remove @available(OSX 10.16, *) from AppDelegate and use if #available(OSX 10.16, *) around the log call as shown below. This ensures that AppDelegate can be instantiated in 10.15 as well, but the logging APIs are used only in 10.16

Code Block
if #available(OSX 10.16, *) {
logger.log("logged Message")
}

Can you please let me know if you are running this app on macOS Big Sur (beta)?
Accepted Answer
I guess the app is running on macOS 10.15. What is happening here is that, since AppDelegate is annotated as NSApplicationMain, it is expected to be available on all deployment targets this app targets. In macOS 10.15, NSApplicationMain() will load the nib, which instantiates the AppDelegate and then runs the applicationDidFinishLaunching() method. Even though AppDelegate has 10.16 only code in it, nib loading will happily instantiate it regardless of the running OS version, which causes this error. Since AppDelegate is marked as only available on 10.16, the compiler doesn’t complain about the use of logger.log.

The solution to this problem is to remove @available(OSX 10.16, *) from AppDelegate and use if #available(OSX 10.16, *) around the log call as shown below. This ensures that AppDelegate can be instantiated in 10.15 as well, but the logging APIs are used only in 10.16

Code Block
if #available(OSX 10.16, *) {
logger.log("logged Message")
}

I was running 10.15 in my example not Big Sur.
I got confused because I thought Big Sur would be called Mac OS 11 beta, not 10.16
Runtime error using example code for logging In XC12
 
 
Q