Swift - Optionals
Given this Swift code, how can you safely print the length of the optional string
text?
var text: String? = "Hello" // Your code here
text?
var text: String? = "Hello" // Your code here
if let safely unwraps the optional text only if it has a value, avoiding crashes.if let block, safeText.count gives the length. If text is nil, print 0.count on optional directly (error). print(text!) force unwraps without check (unsafe). print(text ?? 0) uses nil coalescing but mismatches types.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions