Implicitly Unwrapped Optionals in Swift

While writing a Swift program many times situation arises where the value of the variable is not available at the time when it is declared but is assured to be present by the time it is first accessed.
In these situations as a programmer you know that when you will access the value it would always be there. But you don’t have the value while declaring the variable. If you declare such a variable as an optional, you will need to unwrap the value of such a variable every time you access it. This would clearly be an unnecessary overhead.

This is the ideal situation where you can use implicitly unwrapped optional.
The name itself suggests that the variable of this type is optional, though you need not explicitly unwrap its value while accessing it.
The value is implicitly unwrapped but it does not guarantee that the value would not be nil. As a programmer its your responsibility to make sure that you use implicitly unwrapped optional only in places where the value of the variable is present before it is first accessed. If you try to access the the value of implicitly unwrapped optional, and the value is nil, you will a runtime exception.

For all practical purposes you can use implicitly unwrapped optional as a normal optional variable. For example you can check whether its nil, you can assign nil to it.(But you should probably use normal optionals if these two are really your requirements)

So much of talking. Where is the code?
Here it comes!!

To declare a variable as implicitly unwrapped optional you just need to postfix the variable type with exclamation mark(!) during declaration.(To declare normal optional you postfix type with question mark(?) instead)
If you want to declare an implicitly unwrapped String, you do it like following

var myString : String!

Assign some value to myString

myString = "This is the sample String"

If you want to use myString, you can do so without having to unwrap it explicitly.

let anotherString = myStringprint(myString) 

You can see that we have not unwrapped myString(using exclamation mark or otherwise), and we are able to access its underneath value. In the above code sample can you guess the type of anotherString? Will it be just String or optional String?
It will be optional String. Reason being myString is implicitly unwrapped but still optional and can contain nil value.

As discussed earlier you can perform all the operations that you can do with optionals on implicitly unwrapped optional. For example assign nil to an implicitly unwrapped optional, check whether implicitly unwrapped optional is nil, perform optional chaining etc.

myString = nil  // implicitly unwrapped optional can be assigned nil
if myString != nil { // Checking for nil is allowed with implicitly unwrapped optional
print("Holding some value")
var characterCount = myString?.characters.count // optional chaining  with implicitly unwrapped optionl
print(characterCount) //prints nil

But, if any of the above three is required in your program, you probably need optional variable and not implicitly unwrapped optional. Because the essence of using implicitly unwrapped optional is that you know this value is going to remain present for you when you use it.

The thing one should keep in mind is that if you try to access implicitly unwrapped optional, which is holding nil at the time of access, a runtime error would be triggered.

var myString : String!
var anotherString = myString + "Added part" // runtime error 

As with optionals, if you don’t provide an initial value when you declare an implicitly unwrapped optional variable or property, its value automatically defaults to nil. You can use optional chaining to conditionally perform an operation on an implicitly unwrapped optional expression. If the value is nil, no operation is performed and therefore no runtime error is produced. Optional types that are nested inside a tuple type or a generic type—such as the element types of a dictionary or array—can’t be marked as implicitly unwrapped.

For example

var tuple :(Int!,Int!) // error
var anotherTuple :(Int,Int)! //OK
var yetAnotherTuple :(Int?,Int?) // OK
var anArray :[Int!] // error
var anotherArray :[Int]? // OK
var yetAnotherArray :[Int?] //OK

Sometimes there is no cleaner way than using implicitly unwrapped optional.
I will leave it upto you depending upon your requirements to make use of implicitly unwrapped optional.

Final words.HANDLE WITH CARE!!!

3 thoughts on “Implicitly Unwrapped Optionals in Swift

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s