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      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.