'self' used before all stored properties are initialized

后端 未结 3 1470
故里飘歌
故里飘歌 2020-12-16 09:34

I\'m working through a learn-swift playground and upgrading it to Swift 2.0 as I learn the language. The following code (which likely worked with prior versions of Swift) n

3条回答
  •  粉色の甜心
    2020-12-16 10:14

    Just do:

    private(set) var capitalCity: City!
    

    which gives you the read-only public interface you want.

    I understand that you find var capitalCity: City! contrived. I'd say that the selected answer is truly contrived; it adds lines of code that have no purpose other than resolving a language-related issue. Lines like that are meaningless and meaning is what matters most in code.

提交回复
热议问题