I want to create a function that can check whether or not a person has entered their middle name or if the variable is 'empty'(i.e nil). If it isn't empty it's going to essentially print the persons first, middle and full name, if not it's supposed to ignore the middle name and print out the first and last name if the value is 'nil'(which is is). For whatever reason it's not printing out person2FirstName and person2LastName without the middle name(which is what I want it to do).
I'm doing this to get practice using optionals in Swift. I'm having a TERRIBLE time wrapping my head around it in practice, though I understand that it's a way to safeguard your code in theory. I would appreciate any help you can give.
let person2FirstName: String = "Steve"
let person2MiddleName: String? = "nil"
let person2LastName: String = "Jones"
if person2MiddleName != nil {
"\(person2FirstName) \(person2MiddleName) \(person2LastName)"
} else {
"\(person2FirstName) \(person2LastName)"
}
I keep getting these errors:
main.swift:14:23: warning: string interpolation produces a debug description for an optional value; did you mean to make this explicit?
"\(person2FirstName) \(person2MiddleName) \(person2LastName)"
^~~~~~~~~~~~~~~~~~~
main.swift:14:24: note: use 'String(describing:)' to silence this warning
"\(person2FirstName) \(person2MiddleName) \(person2LastName)"
~^~~~~~~~~~~~~~~~~~
String(describing: )
main.swift:14:24: note: provide a default value to avoid this warning
"\(person2FirstName) \(person2MiddleName) \(person2LastName)"
~^~~~~~~~~~~~~~~~~~
?? <#default value#>
main.swift:14:1: warning: string literal is unused
"\(person2FirstName) \(person2MiddleName) \(person2LastName)"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.swift:16:1: warning: string literal is unused
"\(person2FirstName) \(person2LastName)"