Wednesday, February 8, 2023
HomeiOS Developmentios - Why does a continuing property could get initialized twice in...

ios – Why does a continuing property could get initialized twice in a Swift class?

The mission I am engaged on is a mixture of Swift and Goal-C. This is the snippet:

// ViewController.m
@interface ViewController ()

@property (nonatomic, sturdy) MyModel *mannequin;


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.mannequin = [[MyModel alloc] initWithIntValue:10];

// MyModel.swift
fileprivate class SomeProperty {
    init() {
        print("SomeProperty init")

class MyModel: BaseModel {
    non-public let property = SomeProperty()

// BaseModel.h
@interface BaseModel : NSObject

- (instancetype)initWithIntValue:(int)intValue;
- (instancetype)initWithIntValue:(int)intValue doubleValue:(double)doubleValue;


// 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];


Apparently, I discover when MyModel occasion is initialized, SomeProperty init shall be printed twice, which suggests two SomeProperty situations are created. What’s worse, Debug Reminiscence Graph reveals that there’s a SomeProperty object reminiscence leak. So why is that this and the way can I repair it?



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments