在Objective-C类中使用调试区域时,无法看到Swift对象层次结构 [英] Can't see Swift object hierarchy in debug area when used within Objective-C class
问题描述
任何评论,想法,帮助,预测都将如此有用。
这是我的课程
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 *)数据;
@end
TrialObjectiveCClass.m
#importTrialObjectiveCClass.h
@implementation TrialObjectiveCClass
- (instancetype)initWithArray:(NSArray *)data {
self.first = data [0];
self.second = data [1];
self.third = data [2];
return self;
}
@end
现在来问题。当我使用这些我的ViewController.m中有两个类,其中有以下代码:
#importViewController.h
# importTrialObjectiveCClass.h
#importCodemaster-Swift.h//自动创建的标题在Objective-C
@interface中的Swift代码ViewController()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray * trialArray = @ [@FirstString,@SecondString,@ 99];
//首先Swift部分
TrialSwiftClass * obj = [[TrialSwiftClass alloc] initWithData:trialArray];
NSLog(@%@,obj.first);
NSLog(@%@,obj.second)
NSLog(@%@,obj.third);
//现在Objective-C部分
TrialObjectiveCClass * obj2 = [[TrialObjectiveCClass alloc] initWithArray:trialArray];
NSLog(@%@,obj2.first);
NSLog(@%@,obj2.second);
NSLog(@%@,obj2.third);
}
如果我在最后一个断点NSLog在ViewController.m中,这是我在调试区域看到的:
我的日志显示我的对象的属性的正确值。
为什么我不能看到我的Swift类的层次结构,但可以看到我的Objective-C类?如何解决这个问题?
Swift类中的变量将暴露给ObjC作为属性。目前,本地图视图不显示对象的属性,只显示其ivars。无论课程来自ObjC还是Swift,都是如此。这就是为什么swift变量不会出现在本地视图中。
您可以使用lldb控制台中的expression命令查看对象的属性来解决此问题。例如,在您的示例中,您可以执行以下操作:
(lldb)expr obj.first
(__NSCFConstantString * )$ 0 = 0x00000001002d1498 @FirstString
等。
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:
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?
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.
这篇关于在Objective-C类中使用调试区域时,无法看到Swift对象层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!