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?

nhgrif
  • 61,578
  • 25
  • 134
  • 173
thund
  • 1,842
  • 2
  • 21
  • 31
  • See this link updated with Swift 1.2 http://stackoverflow.com/questions/29570355/assigning-let-variable-in-fallible-initializer-swift-1-2 – Babul Prabhakar Jun 19 '15 at 11:55

2 Answers2

1
let logManager: QCLogManager!
let cloudJobManager: CloudJobManager!

if let is not necessary, change to var

var logManager: QCLogManager!
var cloudJobManager: CloudJobManager!
maquannene
  • 2,257
  • 1
  • 12
  • 10
  • 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. – thund May 26 '15 at 02:27
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
    }
muneeb
  • 2,461
  • 1
  • 13
  • 9
  • 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. – thund Apr 21 '15 at 04:10