Question mark(?) after data type

swift, variables

Solution

The question mark signifies that it may contain either a value, or no value at all. Without it, it cannot ever be `nil`.

var str: String = "Hello"
str = nil // Error

Problem

These two statements make me confused ``` var optionalString: String? = "Hello" ``` Instead we could write it ``` var optionalString: String = "Hello" ``` What's the difference between these two? We can also do this without optional values. ``` var str: String = nil str = "Hello" ``` Please clarify.

Original source

Related problems