Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't see Swift object hierarchy in debug area when used within Objective-C class

My problem may be so simple but I'm so lost in it. Any comment, idea, help, prediction would be so useful.

Here are my classes

TrialSwiftClass.swift

import Foundation

@objc public class TrialSwiftClass : NSObject{
    var first : String?
    var second : NSString?
    var third : NSNumber = 0


    override init(){
        super.init()
    }

    init(data:NSArray){

            self.first = data[0] as? String
            self.second = data[1] as? NSString
            self.third = data[2] as! NSNumber
    }
}

TrialObjectiveCClass.h

#import <Foundation/Foundation.h>

@interface TrialObjectiveCClass : NSObject

@property (nonatomic, strong) NSString *first;
@property (nonatomic, strong) NSString *second;
@property (nonatomic, assign) NSNumber *third;

- (instancetype)initWithArray:(NSArray *)data;

@end

TrialObjectiveCClass.m

#import "TrialObjectiveCClass.h"

@implementation TrialObjectiveCClass


- (instancetype)initWithArray:(NSArray *)data{
    self.first = data[0];
    self.second = data[1];
    self.third = data[2];

    return self;
}
@end

Now here comes the problem.When I use these two classes in my ViewController.m which has following code in it:

#import "ViewController.h"
#import "TrialObjectiveCClass.h"
#import "Codemaster-Swift.h" //Automatically created header to use Swift code in Objective-C


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray *trialArray = @[@"FirstString", @"SecondString", @99];


    //First the Swift part
    TrialSwiftClass *obj = [[TrialSwiftClass alloc] initWithData:trialArray];

    NSLog(@"%@", obj.first);
    NSLog(@"%@", obj.second);
    NSLog(@"%@", obj.third);


    //Now the Objective-C part
    TrialObjectiveCClass *obj2 = [[TrialObjectiveCClass alloc] initWithArray:trialArray];
    NSLog(@"%@", obj2.first);
    NSLog(@"%@", obj2.second);
    NSLog(@"%@", obj2.third);


}

If I put a breakpoint in the last NSLog in ViewController.m, here is what i see in debug area: enter image description here

My logs are showing the right value of my object's properties.

Why can't i see the hierarchy of my Swift class but can see my Objective-C class? How to solve this problem?

like image 893
Okhan Okbay Avatar asked Dec 12 '15 20:12

Okhan Okbay


People also ask

How do you inherit a Swift class in Objective-C?

Swift classes that are inherited from OBJC classes are bridged automatically. That means any class inherited from, for example, UIViewController is automatically seen in the OBJC runtime. If you're creating a class that doesn't inherit from anything, then make it an NSObject subclass, as you would in OBJC.

How do I add Objective-C to Swift?

Add any Objective-C file to your Swift project by choosing File -> New -> New File -> Objective-C File. Upon saving, Xcode will ask if you want to add a bridging header. Choose 'Yes'.

What is LLDB in Xcode?

LLDB is a debugging component used in the LLVM project which was developed by the LLVM developer group. Xcode uses the LLDB as the default debugging tool. The full form of LLDB is Low-level debugger. Breakpoints help a developer to stop the execution of the program at any point.

Can we use OBJC files in Swift project?

To import a set of Objective-C files into Swift code within the same app target, you rely on an Objective-C bridging header file to expose those files to Swift. Xcode offers to create this header when you add a Swift file to an existing Objective-C app, or an Objective-C file to an existing Swift app.


1 Answers

The variables in your Swift class get exposed to ObjC as properties. At present the Locals view does not show the properties of an object, only its ivars. This is true regardless of whether the class comes from ObjC or Swift. That's why the swift variables don't show up in the locals view.

You can work around this by using the expression command in the lldb console to view the object's properties. For instance, stopped in your example, you can do:

(lldb) expr obj.first
(__NSCFConstantString *) $0 = 0x00000001002d1498 @"FirstString"

etc.

like image 112
Jim Ingham Avatar answered Oct 04 '22 02:10

Jim Ingham