Basic Swift features
Having a solid understanding of Swift’s basic concepts is essential, as a lack of knowledge in these areas can cause significant issues for iOS developers, not to mention job interviews.
Answering optionals questions
Optionals are a fundamental concept in Swift and help us to write safe and robust code. These can handle the possibility of a variable having no value (nil). We define optional by adding ? after the variable type.
Here's an example:
var name: String?
In the preceding line of code, name can contain a value or nil.
A simple way to unwrap an optional and extract its value is the if let statement:
var name: String? = "Avi"if let unwrappedValue = name {
print("The unwrapped value is: \(unwrappedValue)")
} else {
print("The optional was nil")
}
As we can see from our code snippet, the if let statement safely “extracts” the value...