swift3 - Assigning value to a variable inside an If else block in swift 3 -


this thing basic sure been asked can't find it.

i experimenting in swift 3 playgrounds , want run code:

import uikit  var optionalvariable: int? = 32 var anothervariable: int if optionalvariable != nil{     print("this executed")     anothervariable = 32 } print(anothervariable) 

it fails run , getting error:

playground execution failed: error: chapter4.playground:5:7: error: variable 'anothervariable' used before being initialized print(anothervariable) 

so what's wrong? why can't use value of anothervariable when have assigned value it?

if comment out last line print(anothervariable), code runs , output printed this executed.

compiler see there path anothervariable not initialized (when optionalvariable nil). happen if e.g. thread sets optional variable nil. won't happen in code compiler can't know this. have initialize anothervariable @ point before pass function.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -