Twitter khuong291
Twitter khuong291

Reputation: 11672

What is diffrence between ? and ? = nil

As the title I've just asked, I want to know which case we should use ? = nil

For example :

var text: String? // text will be initialized with nil

var text: String? = nil // text will be assigned to nil

So what is the diffrence between them. When we should use ? = nil

Upvotes: 0

Views: 70

Answers (1)

Jonathon Ogden
Jonathon Ogden

Reputation: 1582

As it says in the Swift Language documentation:

You use optionals in situations where a value may be absent. An optional says: There is a value, and it equals x or there isn’t a value at all

There is no difference between ? and ? = nil. The ? = syntax allows you to assign a default (a word they should perhaps use in the documentation for the sake of clarity) value of your choosing.

So if there is no value at all: ? or, if there is a value, and it equals x, then: ? = <some value>

Upvotes: 3

Related Questions