Question 1
if let
is knowns as optional binding.
It only works on Optionals. The following code would generate an error:
var str = "Hello, playground"
if let x = str {
print("success") // error: initializer for conditional binding must have Optional type, not 'String'
}
Why? Because if let is only used for optionals, String
is not an optional.
Question2
And yes it evaluates if the optional has been set ie has a value.
For more info see this answer
Additional question: Why are we doing such?
Because optionals are either set or not ( have a value or not). We want to only do something if they have a value. If they don't have a value, we don't want to do anything (we don't want to crash either).
You also use it to unwrap the optional.
var optionalString : String? = "hi"
if let unwraped = optionalString {
print (unwraped) // hi | easier to work with
print(optionalString) // Optional("hi") | not so easy to work with
}