Swift gives "self used before all stored procedures are initialized" error when building child nodes
Asked Answered
P

2

2

In XCode 6.2, I have a Swift project where a main-object ("Backbone") creates sub-objects with pointers back to Backbone:

class Backbone
{
    let logManager: QCLogManager!
    let cloudJobManager: CloudJobManager!
    ...

    init() {
        logManager = QCLogManager(backbone: self)
        cloudJobManager = CloudJobManager(backbone: self)
        ...
    }

It works very nicely. However, in XCode 6.3 each line in init() now gives the error:

'self' used before all stored properties are initialized.

That seems a rather unhelpful compiler restriction. Why did it change? How should I now be doing it?

Phenolphthalein answered 21/4, 2015 at 1:20 Comment(1)
See this link updated with Swift 1.2 #29570855Faradism
H
1
let logManager: QCLogManager!
let cloudJobManager: CloudJobManager!

if let is not necessary, change to var

var logManager: QCLogManager!
var cloudJobManager: CloudJobManager!
Honesty answered 25/5, 2015 at 3:48 Comment(1)
But Backbone.logManager should be immutable, so I want to use let. Making it mutable just to save a line of code seems like a bad trade.Phenolphthalein
D
0

You might need to create a property of type Backbone in both QCLogManager and CloudJobManager and set that property after initialization of all store properties of BackBone class.

class Backbone
{
    let logManager: QCLogManager!
    let cloudJobManager: CloudJobManager!
    ...

    init() {
        logManager = QCLogManager()
        cloudJobManager = CloudJobManager()
        ...
        logManager.backBone = self
        cloudJobManager.backBone = self
    }
Dube answered 21/4, 2015 at 2:35 Comment(1)
Yes, that certainly works. I'm still hoping there might be a cleaner way to do it, but perhaps that's no longer possible. Thanks.Phenolphthalein

© 2022 - 2024 — McMap. All rights reserved.