Marcus Leon
Marcus Leon

Reputation: 56679

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:

@UIApplicationMain
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?

Upvotes: 2

Views: 1147

Answers (2)

Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27133

Also I guess it's better to create global constant file and create something like this (if you've declared log in AppDelegate):

let LOG = (UIApplication.sharedApplication().delegate as! AppDelegate).log

and then simple use LOG.error("error")

Upvotes: 0

Sulthan
Sulthan

Reputation: 130102

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()

@UIApplicationMain
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:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

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

and access it using:

AppDelegate.log.debug(...)

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.

Upvotes: 4

Related Questions