So I have the following lines:
let theUsername = "\(String(describing: selectedPost?.user.username!))" if selectedPost?.user.username! != nil { print("Contains a value!") username.text = theUsername//Fails here } else { print("Doesn’t contain a value.") username.text = "No username found: Weird" } The error message:
Unexpectedly found nil while implicitly unwrapping an Optional value
would make one think the value inside is nil. However when printing:
print("\(String(describing: selectedPost?.user.username!))", " LIT555") I get:
Optional("C22AE009-8CC6-490A-9328-23A08AAD3A10") LIT555
How can I turn the value into a non optional (ie. get rid of the Optional() part), so it can work without it failing?
usernameisnileven ifselectedPostisn't. You need to conditionally unwrap everything - e.g.if let post = selectedPost, let username = post.user.username { ..String(describing:)for anything other than debug output. Never use it to show a value to a user.Optional(...)to the text. You don't want to show that to a user. Properly deal with optionals instead of doing inappropriate things like string interpolation ofString(describing:).