UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Implicit Unwraping

Forums > 100 Days of Swift

Hi Guys,

I am currently on project 7 but I still do not undertsand why in project 4 we implicity uwrapped the webView property.

var = webView: WKWebView!

3      

You are declaring that it there and when you come to use it will be there. Same as when you use var button: UIButton!

and then in the override func loadView() you put

webView = WKWebView()

PS no = in the var webView: WKWebView!

4      

Yeah but why do I need to implicitly unwrap it.

3      

You need to do that to tell Xcode that you're sure it will be there at runtime. If you leave the ! out, you will see Xcode will complain about not having an initialiser. That;s another option, to initialise the property in an initialiser.

Another option is to directly initialise the property like so:

var webView = WKWebView()

You assign a WKWWebView to the var webView and initialise it on the go!

4      

Okay makes sense, thanks guys!

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.