Optionals

Like Kotlin, Swift offers support for optional types. In Swift the null value is nil instead of null.

print("Enter your name:")
val name : String? = readLine()

Swift also offers a safe call operator:

println("Your name has \(name?.length) letters in it.")

The Swift equivalent of the Kotlin Elvis operator is the nil-coalescing operator ??:

print("Your name has $(name?.length ?? 0) letters in it.")

Swift also offers a conditional unwrapping construct that allows you to specify what to do when a value is not nil or nil:

if let actualName = name {
  print("Your name has \(actualName.length) letters in it.")
else {
  print("You did not enter a name.")
}

Force unwrapping

If you are certain that an optional does not contain a null value you can coerce the optional type to a non-optional type through the use of the force unwrapping operator (! in Swift and !! in Kotlin):

In Swift:

val name = readLine() // Type of name is String?
if name != nil {
  print("Your name has \(name!.length) letters in it.")
} else {
  print("You did not enter a name.")
}

In Kotlin:

val name = readLine() // Type of name is String?
if (name != null) {
  print("Your name has \(name!!.length) letters in it.")
} else {
  print("You did not enter a name.")
}