gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:51:31 26 4
gpt4 key购买 nike

尝试设置 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文件中声明,你基本上可以把它放在代码的任何地方)

或使其静态:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

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

并使用以下方式访问它:

AppDelegate.log.debug(...)

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

关于swift - XCGLogger:对成员 'log' 的引用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36923620/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com