Swift Expected String value but found null instead

When you encounter the error “Expected String value but found null instead” in Swift, it typically means that you have attempted to assign a value of type “String?” (an optional string) to a variable or constant of type “String” (a non-optional string).

The solution for Expected String value but found null instead issue is to modify the string property of your struct from

 let description: String

to this:

 let description: String?

i.e we have marked the property as optional.

For instance, the following code would cause this error:

let firstName: String? = nil
let message = "Hello, \(firstName)"

To fix this issue, you can use the nil-coalescing operator (??) to provide a default value for your optional string:

let firstName: String? = nil
let message = "Hello, \(firstName ??  "User")"

Another way to go around that issue is to use the “if let” statement to unwrap an optional string safely:

let firstName: String? = nil
if let firstName = firstName {
    let message = "Hello, \(firstName)"
} else {
    let message = "Hello, user"

When you’re working with optionals, remember to check if they contain a value before using them.