1

I am following the The Swift Programming Language book to investigate strong reference cycle. One of the examples that should be working cannot be compiled in Xcode. I don't understand why the code is broken.

On this chapter, there is an example that looks like this:

This example won't compile in Xcode

When I try to compile this code in Xcode the this error was thrown: 'self' used before all stored properties are initialized. However, I think it should have been able to compile because I set capitalCity to be Implicitly Unwrapped Optionals that is nil by default, so after I set self.name = name all stored properties should be already properly set.

What do I miss here? What changes are needed to make the code compile?

Thanks in advance!

Roy Li
  • 511
  • 1
  • 6
  • 14
  • lazy var capitalCity = { City(..., country: self}() then don't set it in the init – David H May 28 '15 at 21:14
  • Roy Li, just to be honest. Let us know your fix/solution for this example from the book!? But note, you should keep the sense of this example there. – ilnar_al May 28 '15 at 21:42

3 Answers3

4

let statements don't have default initialization of optionals to nil, because otherwise writing let foo:Bar! would give you a foo that was always nil and you couldn't initialize it in a subsequent statement.

The reason var is appropriate is that you want default initialization to nil to occur so you can initialize your City object with the self reference in order to finally initialize your actual capitalCity value which is really double initialization.

The code has a circular class dependency by design, so this is a side effect of that design.

This behavior of let is new in Swift 1.2, try the example in Xcode 6.2 or earlier and you will find that it compiles.

sylvanaar
  • 8,096
  • 37
  • 59
  • Interesting that the book in question was actually updated for Swift 1.2, as there described Swift's Sets etc. But they just didn't fix the previous code. Anyway, my initial answer was right! I can only admit that I shouldn't write about setting up let in initializer, but it was obvious actually! – ilnar_al May 28 '15 at 22:34
2

As the initializer has not set up the object yet, you cannot initialise another one with it.

You need to initialize the city and then set the property.

some_id
  • 29,466
  • 62
  • 182
  • 304
0

Just Change (let to var)

let capitalCity: City!

To

var capitalCity: City!

This happens, probably, because it cannot be a constant value, since we set it up during init.

update:

I have NO idea why I'm down voted!. This is the example from the book, and does NOT compile. But if you change let to var as I said, you'll get it work!

Who can offer here a code modification, so that the code FROM THE BOOK can be compiled!?

I can only admit that I shouldn't write about setting up let in initializer, but I think it's obvious I didn't mean that you cannot set up regular let in initialiser

ilnar_al
  • 932
  • 9
  • 13
  • No, you can initialize a constant in initializers. – NobodyNada May 28 '15 at 21:21
  • But sometimes you really need a constant correct? I think we need a way to do that appropriately instead of taking shortcut like this. – Roy Li May 28 '15 at 21:21
  • You are right @RoyLi, let still applies until you set the value. – some_id May 28 '15 at 21:22
  • This is the example from the book, and does NOT compile. But if you change let to var as I said, you'll get it work! – ilnar_al May 28 '15 at 21:22
  • To NobadyNada this is an OPTIONAL constant – ilnar_al May 28 '15 at 21:25
  • To Roy Li, I also read the same chapter from the Swift book and encountered the same problem (it does not compile). And if you ONLY change let to var you get it work. It should work with let, but DON'T – ilnar_al May 28 '15 at 21:32
  • could you post a link to which book and where? I'd be more inclined to cast an upvote. – Dan Beaulieu May 28 '15 at 21:43
  • https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20-ID51 This is ARC in Swift example with City/Country – ilnar_al May 28 '15 at 21:45
  • similar topic related to changes in swift 1.2 http://stackoverflow.com/questions/29956702/unable-to-declare-swift-implicitly-unwrapped-optional-as-a-constant – Steve May 28 '15 at 23:07