Question about making a variable non-conditional from a point onwards

I am still fairly new to Swift, so please bear with me.

I have this piece of code:

Code Block var win_name : String? = winInfo[kCGWindowName as String] as? Stringif ( win_name == nil ){win_name = winInfo[kCGWindowOwnerName as String] as? String}if ( win_name == nil ){	 win_name = "???"}// from here on, we know win_name is non-nil

After the last line, there are many more lines where I need to do access win_name lots of times. Each time I have to write win_name! .

Is there a way to tell the compiler that after line 10, win_name is always non-nil, so that I don't have to force-unwrap it every time?

I could use something like
Code Block guard let win_name_nonnil = win_name else { return }

at line 10, but IMHO that would make the code after line 10 even uglier.

Insights will be highly appreciated.
Answered by OOPer in 620399022
In your case, nil-coalescing operator ?? would work.

Code Block let win_name : String = winInfo[kCGWindowName as String] as? String		?? winInfo[kCGWindowOwnerName as String] as? String		?? "???"


Accepted Answer
In your case, nil-coalescing operator ?? would work.

Code Block let win_name : String = winInfo[kCGWindowName as String] as? String		?? winInfo[kCGWindowOwnerName as String] as? String		?? "???"


Question about making a variable non-conditional from a point onwards
 
 
Q