Purpose of ?? in Swift -


this question has answer here:

can explain advantage of writing ??.would great if possible explain below code snippet how improve code?

import foundation var samplestring:string? print(samplestring ?? "nil") 

thanks in advance.

from apple documentation:

the nil-coalescing operator (a ?? b) unwraps optional if contains value, or returns default value b if nil. expression of optional type. expression b must match type stored inside a.

in case if samplestring nil return default string containing text "nil".


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -