我的问题可能很简单,但我迷失了.任何评论,想法,帮助,预测都会非常有用.
这是我的课程
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@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
现在问题来了.当我在我的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); }
如果我在ViewController.m的最后一个NSLog中放置一个断点,这是我在调试区域中看到的:
我的日志显示了对象属性的正确值.
为什么我看不到我的Swift类的层次结构,但可以看到我的Objective-C类?如何解决这个问题呢?
Swift类中的变量作为属性暴露给ObjC.目前,Locals视图不显示对象的属性,仅显示其ivars.无论该类是来自ObjC还是Swift,都是如此.这就是为什么swift变量不会出现在locals视图中.
您可以使用lldb控制台中的expression命令来查看对象的属性.例如,在您的示例中停止,您可以执行以下操作:
(lldb) expr obj.first (__NSCFConstantString *) $0 = 0x00000001002d1498 @"FirstString"
等等