XCGLogger:对成员的引用不明确 'log'

XCGLogger: Ambiguous reference to member 'log'

正在尝试设置 XCGLogger 并收到错误:

Ambiguous reference to member 'log'

我看到这个问题已经 raised 但我不清楚解决方案..

根据安装指南将此全局常量添加到 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
    }

然后在各个源文件中:

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

正确的用法是什么?

问题比较简单。如果你在 AppDelegate 中声明 log,你就是在创建一个实例变量。要访问它,您必须将其作为实例变量访问:

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

如果您希望 log 随处可见,您必须将其设置为全局常量:

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

let log = XCGLogger.defaultInstance()

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

(不需要在AppDelegate文件中声明,基本上可以放在代码的任何地方)

static:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

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

并使用以下方式访问它:

AppDelegate.log.debug(...)

为了解释歧义引用,有一个叫做log的数学函数,malloc.h文件中也有一个log函数。由于您将 String 作为第一个参数传递,并且这两个函数都不匹配,因此编译器会警告您它不知道您要使用这两个函数中的哪一个。

另外我想最好创建全局常量文件并创建类似这样的文件(如果您已声明登录 AppDelegate):

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

然后简单使用LOG.error("error")