Marcus Leon Marcus Leon - 1 year ago 124
Swift Question

XCGLogger: Ambiguous reference to member 'log'

Trying to set up XCGLogger and receiving error:

Ambiguous reference to member 'log'

I see this issue was already raised but I'm not clear on the solution..

Per the install guide added this global constant to AppDelegate.swift:

class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let log = XCGLogger.defaultInstance()

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
log.setup(.Debug, showThreadName: true, showLogLevel: true, showFileNames: true, showLineNumbers: true, writeToFile: nil, fileLogLevel: .Debug)
return true

Then in individual source files:

import XCGLogger
log.debug("A debug message")

What is the proper usage?

Answer Source

The issue is rather simple. If you declare log inside AppDelegate, you are making an instance variable. To access it, you will have to access it as instance variable:

(UIApplication.sharedApplication().delegate as! AppDelegate).log.debug("test")

If you want log to be accessible everywhere, you will have to make it a global constant:

In your AppDelegate, declare a global constant to the default XCGLogger instance.

let log = XCGLogger.defaultInstance()

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

(there is no need to declare it in AppDelegate file, you can basically put it anywhere in your code)

or make it static:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    static let log = XCGLogger()

and access it using:


To explain the ambigious reference, there is a mathematical function called log, and there is also a log function in the malloc.h file. Since you are passing a String as the first parameter and neither of the two functions is a match, the compiler warns you that it does not know which of the two functions you want to use.