Optionals

Declaring
var myString: String?

To check if myString is null

if let myStringValue = myString
{
//will only run if there is a value in myString
}


Note:
- Prevents null references
- Does away with checking for null references
- The question mark indicates that it can contain either nothing or a variable
- When you try to print it, 'nil' will appear
- If it is a null value and you try to append a string to it, you will get an error.
- If there is an error while referencing a null value, swift might add an '!' to the back of the variable, but you should just remove it.


Comments

Popular posts from this blog

Setting up a playground

Go to another page