Why doesn't Swift allow setting value of an optional constant after object initialization?

前端 未结 2 1661
旧巷少年郎
旧巷少年郎 2021-01-29 06:51

The code below creates a compile error saying \"error: return from initializer without initializing all stored properties (\'self.response\' not initialized)\"

c         


        
2条回答
  •  执笔经年
    2021-01-29 07:42

    Optional variables / properties are automatically set to nil by definition if no initial value is provided in the declaration line.

    An optional constant is stuck to nil which makes no sense...

    Therefore the compiler doesn't let you declare an optional constant this way.

提交回复
热议问题