问题
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.
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.
Example classes:
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);
}
Swift Class
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"
}
Generated Header for Swift class
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
The output of the logs is as follows :
Property: apple
Return: apple
Object: <SOQuestion.SOSingleton: 0x60800003e140>
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
回答1:
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.
In the meantime, you can probably work around it by manually specifying Swift in the lldb console:
expr -l swift -O -- SOSingleton.instance.apple
Or, if you need to do something with it in Objective-C:
expr -l swift -O -- SOSingleton.instance
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]
来源:https://stackoverflow.com/questions/46432122/cant-log-swift-singletons-from-objective-c-in-xcode-console