无法在Xcode Console中从Objective-C记录Swift Singletons [英] Can't log Swift Singletons from Objective-C in Xcode Console
问题描述
我在Xcode项目中遇到了控制台问题.我可以在Xcode 8和9,Swift 3和4中在Swift内调试Swift Singleton,但不能在Objective-C内调试.
I have come across an issue with the console in my project in Xcode. I am able to debug a Swift Singleton inside Swift but not Objective-C, in Xcode 8 and 9, Swift 3 and 4.
问题是,当调试Objective-C类时,为什么不能在控制台中打印所讨论的值?在Swift类中进行调试时没有问题,控制台甚至为我自动完成了swift类.
The question is, why can't the values in question be printed in the console when debugging an Objective-C class? There is no issue when debugging in a Swift class, and the console even auto completes the swift class for me.
示例类:
Objective-C View Controller
#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类的生成的标题
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时):
The output of logging is as follows (while debugging the 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
推荐答案
似乎您已发现lldb中的错误.我可能会在 http://bugs.swift.org 上提交错误报告,以使开发团队知道
It looks like you've discovered a bug in lldb. I'd probably submit a bug report on http://bugs.swift.org to let the development team know.
同时,您可以通过在lldb控制台中手动指定Swift来解决此问题:
In the meantime, you can probably work around it by manually specifying Swift in the lldb console:
expr -l swift -O -- SOSingleton.instance.apple
或者,如果您需要在Objective-C中做一些事情:
Or, if you need to do something with it in Objective-C:
expr -l swift -O -- SOSingleton.instance
将会输出类似<SOQuestion.SOSingleton: 0x0123456789abcdef>
的内容,此时您可以复制十六进制值并执行类似的操作:
which will output something like <SOQuestion.SOSingleton: 0x0123456789abcdef>
, at which point you can copy the hex value and do something like:
po [(id)0x0123456789abcdef apple]
这篇关于无法在Xcode Console中从Objective-C记录Swift Singletons的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!