How can you make a property optional in Swift?

An optional in Swift is a type that can hold either a value or no value. Optionals are written by appending a ? to any type:

Example

var name: String? = "Online Interview Questions"

Declaring a question mark "?" in the code can make a property optional.
If a property doesn’t hold a value, then this symbol "?" helps in avoiding the runtime errors.

Comments

Popular posts from this blog

In Swift, Optionals and Optional Binding are key concepts for handling values ?

What is benifit of using higher order functions?

Swift Optionals and force unwrapping?