2

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
Charles Srstka
  • 16,665
  • 3
  • 34
  • 60
Breadbin
  • 410
  • 4
  • 15

1 Answers1

2

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]
Charles Srstka
  • 16,665
  • 3
  • 34
  • 60
  • Thanks Charles, I'll submit a report. The expression example above appears to suffer from a similar problem - it returns `error: :3:1: error: use of unresolved identifier 'SOSingleton'` . However, i was able to use the value logged from before to achieve your po example, so thanks for that! – Breadbin Sep 27 '17 at 08:43
  • 1
    @Breadbin You might need to import first for lldb to become aware of the symbols in your project: `expr -l swift -- import TheNameOfMyProject` – Charles Srstka Sep 27 '17 at 15:31