我正在进行的项目是SWIFT和OBJECT-C的混合体.以下是其中的一小段:
// ViewController.m
@interface ViewController ()
@property (nonatomic, strong) MyModel *model;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.model = [[MyModel alloc] initWithIntValue:10];
}
// MyModel.swift
fileprivate class SomeProperty {
init() {
print("SomeProperty init")
}
}
class MyModel: BaseModel {
private let property = SomeProperty()
}
// BaseModel.h
@interface BaseModel : NSObject
- (instancetype)initWithIntValue:(int)intValue;
- (instancetype)initWithIntValue:(int)intValue doubleValue:(double)doubleValue;
@end
// BaseModel.m
@implementation BaseModel
- (instancetype)initWithIntValue:(int)intValue doubleValue:(double)doubleValue {
if (self = [super init]) {
}
return self;
}
- (instancetype)initWithIntValue:(int)intValue {
return [self initWithIntValue:intValue doubleValue:0];
}
@end
有趣的是,我发现当初始化MyModel个实例时,SomeProperty init将打印两次,这意味着创建了两个SomeProperty实例.更糟糕的是,调试内存图显示存在SomeProperty对象内存泄漏.那么,为什么会出现这种情况,我如何才能修复它呢?