NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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!

   

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!

1      

Yeah but why do I need to implicitly unwrap it.

   

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!

1      

Okay makes sense, thanks guys!

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED Building and maintaining in-app subscription infrastructure is hard. Luckily there's a better way. With RevenueCat, you can implement subscriptions for your app in hours, not months, so you can get back to building your app.

Try it for free

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

Reply to this topic…

You need to create an account or log in to reply.

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.