Introduction
Swift is Apple's language for its platforms: iPhone, iPad, Mac, Apple Watch, and Apple TV. Introduced in 2014 to replace the older Objective-C, it was designed to be safe, fast, and β unusually for a systems language β genuinely pleasant to read and write.
Even if you never ship an app, Swift teaches one idea every developer should understand: optionals.
Optionals: saying "this might be missing"
Most bugs in app development come from one cause: code assuming a value exists when it does not. Swift makes that assumption impossible to hide. A value that might be missing is wrapped in an Optional, and you must unwrap it before use.
var nickname : String ? = nil nickname = "Waqas" if let name = nickname { print ( "Hello, \( name ) !" ) } else { print ( "No nickname set." ) }
Enter fullscreen mode
Exit fullscreen mode
String? means "a String, or nothing". The if let unwraps it safely: inside the block, name is guaranteed to exist.β¦