When should I use optionals and when should I use non-optionals with default values?

后端 未结 2 1470
南方客
南方客 2021-01-03 02:14

I know the recommended way in Swift is to use:

class Address {
var firstLine : String?
var secondLine : String?
}

but sometimes I see other

2条回答
  •  时光取名叫无心
    2021-01-03 02:53

    Well you should use optionals if you think that the variable might not have a value. But if you're really sure that it's gonna have a value then you don't need to use them.

    So only use non-optionals if you're sure that the variable will have a value else use optionals.

提交回复
热议问题