在Objective-C类中使用时,无法在调试区域中看到Swift对象层次结构

时间:2022-05-14 23:53:01

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

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

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

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:

现在问题来了。当我在我的ViewController.m中使用这两个类时,其中包含以下代码:

#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: 在Objective-C类中使用时,无法在调试区域中看到Swift对象层次结构

如果我在ViewController.m的最后一个NSLog中放置一个断点,这是我在调试区域中看到的:

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?

为什么我看不到我的Swift类的层次结构,但可以看到我的Objective-C类?如何解决这个问题呢?

2 个解决方案

#1


5  

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.

Swift类中的变量作为属性暴露给ObjC。目前,Locals视图不显示对象的属性,仅显示其ivars。无论该类来自ObjC还是Swift,都是如此。这就是swift变量没有出现在locals视图中的原因。

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控制台中使用expression命令来查看对象的属性来解决此问题。例如,在您的示例中停止,您可以执行以下操作:

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

etc.

等等

#2


0  

You can't see the hierarchy of your Swift class in that point. When you bridge your swift class to an obj-c file something happening and the debugger can't see anymore your properties. This is a bug or something else, I can't figure out yet. But if you would like to see your hierarchy you can see that in your Swift file.

在这一点上,您无法看到Swift类的层次结构。当您将swift类桥接到obj-c文件时发生了一些事情,调试器再也看不到您的属性了。这是一个错误或其他什么,我还想不通。但是如果你想看到你的层次结构,你可以在你的Swift文件中看到它。

在Objective-C类中使用时,无法在调试区域中看到Swift对象层次结构

在Objective-C类中使用时,无法在调试区域中看到Swift对象层次结构

Another solution if you create a swift project and initialize there the objective-c classes. So if you can create on the opposite way, the properties and hierarchy will appear in the debug field.

另一种解决方案,如果您创建一个swift项目并在那里初始化objective-c类。因此,如果您可以以相反的方式创建,则属性和层次结构将显示在调试字段中。

在Objective-C类中使用时,无法在调试区域中看到Swift对象层次结构

This is not an explanation for that, why your solution not working but I hope this will help you a little bit.

这不是解释,为什么你的解决方案不起作用,但我希望这会对你有所帮助。

#1


5  

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.

Swift类中的变量作为属性暴露给ObjC。目前,Locals视图不显示对象的属性,仅显示其ivars。无论该类来自ObjC还是Swift,都是如此。这就是swift变量没有出现在locals视图中的原因。

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控制台中使用expression命令来查看对象的属性来解决此问题。例如,在您的示例中停止,您可以执行以下操作:

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

etc.

等等

#2


0  

You can't see the hierarchy of your Swift class in that point. When you bridge your swift class to an obj-c file something happening and the debugger can't see anymore your properties. This is a bug or something else, I can't figure out yet. But if you would like to see your hierarchy you can see that in your Swift file.

在这一点上,您无法看到Swift类的层次结构。当您将swift类桥接到obj-c文件时发生了一些事情,调试器再也看不到您的属性了。这是一个错误或其他什么,我还想不通。但是如果你想看到你的层次结构,你可以在你的Swift文件中看到它。

在Objective-C类中使用时,无法在调试区域中看到Swift对象层次结构

在Objective-C类中使用时,无法在调试区域中看到Swift对象层次结构

Another solution if you create a swift project and initialize there the objective-c classes. So if you can create on the opposite way, the properties and hierarchy will appear in the debug field.

另一种解决方案,如果您创建一个swift项目并在那里初始化objective-c类。因此,如果您可以以相反的方式创建,则属性和层次结构将显示在调试字段中。

在Objective-C类中使用时,无法在调试区域中看到Swift对象层次结构

This is not an explanation for that, why your solution not working but I hope this will help you a little bit.

这不是解释,为什么你的解决方案不起作用,但我希望这会对你有所帮助。