3

我正在将使用 Lumberjack Logging 的 Objective-C 文件转换为 Swift。除了我声明的部分之外,它似乎大部分都在工作ddloglevel

执行此操作的 Objective-C 方法:

#ifdef DEBUG
static const int ddLogLevel = LOG_LEVEL_INFO;
#else
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#endif

迅速的方式:

#if DEBUG
let ddLogLevel = LOG_LEVEL_INFO;
#else
let ddLogLevel = LOG_LEVEL_VERBOSE;
#endif

除了我是这个编译时错误: Use of unresolved identifier 'LOG_LEVEL_INFO'

为什么会这样?我该如何解决?

4

2 回答 2

8

您可以使用解决方法。您可以将日志级别显式设置为所有 Logger,而不是全局设置 logLevel(仅在 Objective-C 中可能)。例子:

class LoggerFactory {
#if DEBUG
  static let defaultLogLevel: DDLogLevel = DDLogLevel.All
#else
  static let defaultLogLevel: DDLogLevel = DDLogLevel.Info
#endif

static func initLogging() {
  DDLog.addLogger(DDTTYLogger.sharedInstance(), withLevel: defaultLogLevel)
  DDLog.addLogger(DDASLLogger.sharedInstance(), withLevel: defaultLogLevel)
}
于 2016-06-01T14:38:05.633 回答
2

查看库源代码,LOG_LEVEL_INFO以及Swift 不会自动导入LOG_LEVEL_VERBOSE的宏。#defineSwift 只看到const's.

但是,我认为您的方法作为一个整体可能没有意义 - 看起来您正在尝试为全局 Objective-C 常量分配一个值ddLogLevel。Swiftlet不会为你做这件事——它是一个完全不同的命名空间。这是因为不能在 Swift 中使用 Objective-C 宏。

你最好的选择是在你的项目中留下一个 Objective-C 文件(比如说,LoggingConfig.m它只包含:

#import "DDLog.h"

#ifdef DEBUG
static const int ddLogLevel = LOG_LEVEL_INFO;
#else
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#endif

您使用的库严重依赖于 Objective-C 的特性,所以最好只使用 Objective-C 来配置它。

编辑:更详细地查看这个库(我以前从未听说过),在 Swift 中使用 CocoaLumberjack 可能不会奏效,因为它的主要 API 是一个名为DDLog. 我不认为这是一场好比赛。

于 2014-07-13T01:22:49.313 回答