-1

我正在尝试在我的 swift 项目中使用LogglyLogger-CocoaLumberjack 。

我在 xCode 中收到此错误。

Enum case 'verbose' has no associated values

我无法解决这个问题。

https://prnt.sc/uznr01

我实际上是在尝试在 swift 5 中翻译 Objective-C 代码。这是我的 swift 函数

在 appDelegate.swift 类中

func initLoggly(){
        

        //      static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
                let ddLogLevel:DDLogLevel = .verbose
                
        //      LogglyLogger *logglyLogger = [[LogglyLogger alloc] init];
                let logglyLogger = LogglyLogger()
        
        //      [logglyLogger setLogFormatter:[[LogglyFormatter alloc] init]];
                logglyLogger.logFormatter = LogglyFormatter()
        
        //      logglyLogger.logglyKey = @"your-loggly-api-key";
                logglyLogger.logglyKey = "XXXXXXXXXXXX-XXXXXX"
                
        //
        //        // Set posting interval every 15 seconds, just for testing this out, but the default value of 600 seconds is better in apps
        //        // that normally don't access the network very often. When the user suspends the app, the logs will always be posted.
        //      logglyLogger.saveInterval = 15;
                logglyLogger.saveInterval = 15
        
        //      [DDLog addLogger:logglyLogger];
                DDLog.add(logglyLogger)
                
        //        // Do some logging
        //      DDLogVerbose(@"{\"myJsonKey\":\"some verbose json value\"}");
        //     ddLogLevel.verbose("{\"initloggly\":\"some verbose json value\"}") // also tried this, error ==> Enum case 'verbose' cannot be used as an instance member
                DDLogLevel.verbose("{\"initloggly\":\"some verbose json value\"}") // Here is the error on this line

    }

请指出我做错了什么!

4

1 回答 1

1

这个库很大程度上基于 Swift 无法访问的 C 预处理器宏。

您可能需要在 Objective-C 中编写一小组使用这些宏的包装函数,这些函数又可以从 Swift 调用。

这是一个示例:

LogglyWrapper.h:

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN
@interface LogglyWrapper : NSObject
+(void) logVerbose:(NSString*) msg;
@end
NS_ASSUME_NONNULL_END

和 LogglyWrapper.m:

#import "LogglyWrapper.h"
#import <LogglyLogger.h>

@implementation LogglyWrapper
static const DDLogLevel ddLogLevel = DDLogLevelVerbose;

+(void) logVerbose:(NSString*) msg {
    DDLogVerbose(@"%@", msg);
}
@end

Swift 的用法:

LogglyWrapper.logVerbose("foo")
于 2020-10-15T11:34:51.953 回答