不能从在Xcode控制台Objective-C的日志斯威夫特单身(Can't log Swi

2019-10-29 07:24发布

我所遇到的问题,在我的Xcode项目的控制台。 我能够调试斯威夫特辛格尔顿内斯威夫特而不是Objective-C的,在Xcode 8和9,斯威夫特3和4。

现在的问题是,为什么不能有问题的值来在控制台调试Objective-C类时印刷? 有一个雨燕类调试时没有问题,控制台甚至自动完成迅速类我。

实施例类:

Objective-C的视图控制器

#import "ViewController.h"
#import "SOQuestion-Swift.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"Property: %@", SOSingleton.instance.appleProperty);
    NSLog(@"Return: %@", [[SOSingleton instance] apple]);
    SOSingleton *so = [SOSingleton instance];
    NSLog(@"Object: %@", so);
}

斯威夫特类

import Foundation

@objcMembers
public class SOSingleton: NSObject {

    public static let instance = SOSingleton()

    public override init() {
        super.init()
    }

    public func apple() -> String {
        return "apple"
    }

    public let appleProperty = "apple"
}

雨燕为类生成的头

SWIFT_CLASS("_TtC10SOQuestion11SOSingleton")
@interface SOSingleton : NSObject
SWIFT_CLASS_PROPERTY(@property (nonatomic, class, readonly, strong) SOSingleton * _Nonnull instance;)
+ (SOSingleton * _Nonnull)instance SWIFT_WARN_UNUSED_RESULT;
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
- (NSString * _Nonnull)apple SWIFT_WARN_UNUSED_RESULT;
@property (nonatomic, readonly, copy) NSString * _Nonnull appleProperty;
@end

日志的输出如下所示:

Property: apple
Return: apple
Object: <SOQuestion.SOSingleton: 0x60800003e140>

记录的输出是如下(在调试的ViewController):

(lldb) po [[SOSingleton instance] apple]
Error [IRForTarget]: Couldn't resolve the class for an Objective-C 
static method call
error: The expression could not be prepared to run in the target

Answer 1:

看起来你已经发现了LLDB的错误。 我可能会提交一个bug报告http://bugs.swift.org让开发团队知道。

在此期间,你也许可以解决它通过在LLDB控制台手动指定斯威夫特:

expr -l swift -O -- SOSingleton.instance.apple

或者,如果您需要在Objective-C来用它做什么:

expr -l swift -O -- SOSingleton.instance

这将输出类似<SOQuestion.SOSingleton: 0x0123456789abcdef>此时您可以复制的十六进制值,做一些这样的:

po [(id)0x0123456789abcdef apple]


文章来源: Can't log Swift Singletons from Objective-C in Xcode Console