Unexpectedly found nil while unwrapping an Optional value

Hi all, I have a UserAPI which takes in a User object and saves it to default or loads it from default. I get a fatal error when using this statement: ‘var UserDataEncoded: [NSData] = userDefaults.object(forKey: “UserItem”) as! [NSData]’. The error is ‘Unexpectedly found nil while unwrapping an Optional value’. I looked around on how to fix this, but I was thoroughly confused. Any help would be greatly appreciated and beneficial, as I’m fairly new to swift. Thanks!

You are forcing a typecast on the return of userDefaults.object(forKey: “UserItem”), but that method is returning nil, because there’s nothing stored for that key.

(If there was a value for that key, and it wasn’t an array of NSData, you’d get a different runtime error for the failed cast.)