1

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?

6
  • You still have a force unwrap there - The ! at the end. This will cause a crash if username is nil even if selectedPost isn't. You need to conditionally unwrap everything - e.g. if let post = selectedPost, let username = post.user.username { .. Commented Apr 9, 2019 at 23:22
  • 2
    There's no conditional binding in the code you posted. Commented Apr 9, 2019 at 23:25
  • And never use String(describing:) for anything other than debug output. Never use it to show a value to a user. Commented Apr 9, 2019 at 23:26
  • @rmaddy why is that? Commented Apr 9, 2019 at 23:28
  • Because its result isn't documented and it does things like add 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 of String(describing:). Commented Apr 9, 2019 at 23:35

2 Answers 2

2

Try

if let res = selectedPost?.user.username { // to unwrap the optional value // check textfield is not nil username.text = res } else { username.text = "default" } 

Or shortly

username.text = selectedPost?.user.username ?? "default" 

According to the crash your textfield is nil , check the IB connection


When you do this

var selectedPost : Post? { didSet { getMediaStats() loadP3Data() } } 

and assign a value to selectedPost in the prepare method of the previous vc , the destination vc outlets are still nil because it's not loaded yet and since didSet is called instantly before the load , hence the crash

So remove didSet and call what inside it in viewDidLoad

Sign up to request clarification or add additional context in comments.

11 Comments

It is connected just checked. I still get Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value. Despite res holding a value...
Before the if statement try print(username.text) check whether it'll crash or not
username.text = selectedPost?.user.username ?? "default" yielded the same error. And the print statement outputted : Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
Then there is no way your username outlet isn't connected , make sure it's connected in that vc , try to disconnect and reconnect to that active vc , may be you assign the vc to many scenes
I updated teh question. I will also do what you just recommended
|
1

Even thought you're using ! at the username, this expression is still optional because you have the ? in selectedPost. I strongly suggest you to never use forced unwraps (!), always use if let or guard let to make sure you can unwrap an optional and avoid those crashes.

This is what your code could look like:

if let theUsername = selectedPost?.user.username { username.text = theUsername } else { username.text = "No username found: Weird" } 

or

username.text = selectedPost?.user.username ?? "No username found: Weird" 

Comments