Unome Unome - 5 months ago 25
Swift Question

EXC_BAD_INSTRUCTION on an optional AFTER validating the optional worked

I'm very confused. Below I have the following happening.

enter image description here
As you can see, I check if userOfProfile is not nil before unwrapping. But then I get the following in my console.


fatal error: unexpectedly found nil while unwrapping an Optional value


I also get the following in my stack trace after the error:

0x10f67b538 <+72>: leaq 0x4d1e1(%rip), %r8 ; partial apply forwarder for Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, file : Swift.StaticString, line : Swift.UInt, flags : Swift.UInt32) -> Swift.Never).(closure #2)
0x10f67b53f <+79>: movq %r12, %rsi
0x10f67b542 <+82>: movq %r13, %rdx
0x10f67b545 <+85>: movl -0x2c(%rbp), %ecx
0x10f67b548 <+88>: movq %rax, %r9
0x10f67b54b <+91>: callq 0x10f54de60 ; function signature specialization <preserving fragile attribute, Arg[1] = [Closure Propagated : reabstraction thunk helper from @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> () to @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> (@out ()), Argument Types : [@callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> ()]> of generic specialization <preserving fragile attribute, ()> of Swift.StaticString.withUTF8Buffer <A> ((Swift.UnsafeBufferPointer<Swift.UInt8>) -> A) -> A


Any ideas on what to do would be appreciated. I'm pretty confused.

var userOfProfile:User?

func loadProfile(_ user: User){
self.userOfProfile = user

if self.userOfProfile != nil{
self.profileView.firstName = self.userOfProfile!.firstName
}else{
self.profileView.firstName = ""
}
...
}


...
//Init for the user object that was passed in.

let entityDescription = NSEntityDescription.entity(forEntityName: "User", in: self.managedObjectContext!)
let matchedUser = User(entity: entityDescription!, insertInto: nil)
matchedUser.parseRaw(matchDict)


....

class User: NSManagedObject {

@NSManaged var userId: String
@NSManaged var dbId: String
@NSManaged var aboutMe: String
@NSManaged var firstName: String
...


The first name property isn't optional. I can even print out the value for firstName by doing
po self.userOfProfile!.firstName
in the console, and it resolves normal..

Look, resolves fine in debugger, crashes on next line. what the heck.

enter image description here

Let me know if any other code would be helpful.

UPDATE: The issue is the profileView being nil, so setting profileView.firstName is causing a crash.

What I do not understand is why profileView is nil. The code to new up profileView looks like this:

//Event that causes all this
let newProfile = ProfileViewController()
newProfile.matchedUserProfile = true
newProfile.matchedReportDelegate = self
newProfile.loadProfile(self.selectedUser!)


//Within ProfileViewController
override func viewDidLoad() {
super.viewDidLoad()

profileView = ProfileView(frame: self.view.frame)
self.view = profileView


In the viewDidLoad for the ProfileViewController, the ProfileView is created. I've followed the stack trace and loadProfile is getting called prior to the viewDidLoad. How do I avoid this? I have many other spots in the code where I do this and it does not crash (this didn't use to crash a couple weeks ago and in our prod version)

Answer Source

if firstName property of userOfPrifile is optional then it will produce an error at runtime if a value is a nil and you try to forcely unwrap the value. so, use if let to construct to safely unwrap.

if let firstName = self.userOfProfile?.firstName {
  self.profileView.firstName = firstName
} else {
  self.profileView.firstName = ""
}

Updated

I Guess your profileView is nil. please check the profileView at runtime.