dhaval shah dhaval shah - 8 months ago 35
Swift Question

Swift, unexpectedly found nil while unwrapping an Optional value (NSKeyedUnarchiver)

I want to store an Array in caching by below way.
But somehow I am getting the error "unexpectedly found nil while unwrapping an Optional value"

My Code Below

Load from Cache

func loadCachedContacts(){
let loadNSCachedContacts = NSUserDefaults.standardUserDefaults().objectForKey(constants.defaultsKeys.VISITED_CONTACTS_KEY) as? NSData
if let genericsData = loadNSCachedContacts {

if let conArray = NSKeyedUnarchiver.unarchiveObjectWithData(genericsData) as?
[Generic] //**error comes here**

//if(conArray != nil){
// if let testArray = conArray {
for generic in conArray {
print("\(generic.genericCode), Cached Storage")

Save into Caching

func storeVisitedContact(generic: Generic)
let def = NSUserDefaults.standardUserDefaults()
var readArray : [Generic] = []
// visitedContactsArr.append(generic)

if var testArray = def.objectForKey(constants.defaultsKeys.VISITED_CONTACTS_KEY) as? [Generic] {
// readArray = testArray! as! [Generic]

if testArray.count>=constants.defaultsKeys.VISITED_CONTACTS_COUNT
readArray = testArray

let archivedObject = NSKeyedArchiver.archivedDataWithRootObject(readArray as [Generic])
NSUserDefaults.standardUserDefaults().setObject(archivedObject, forKey: constants.defaultsKeys.VISITED_CONTACTS_KEY)

Can someone please help me with some solutions?
Editing with NSCoding protocol

class Generic: NSObject, NSCoding

var genericCode = String("")
var genericName = String("")
var genericType = String("")
var genericImageUrl = String("")
var genericPhone = String("")
var orgName = String("")

var const = Constants()

override init()


//Parameterzed Constructor for the Generic
init(genericCode: String , genericName: String , genericPhone: String, genericType: String, genericImageUrl : String)
self.genericCode = genericCode
self.genericName = genericName
self.genericType = genericType
self.genericImageUrl = genericImageUrl
self.genericPhone = genericPhone


required init(coder aDecoder: NSCoder) {
genericCode = aDecoder.decodeObjectForKey("genericCode") as! String
genericName = aDecoder.decodeObjectForKey("genericName") as! String
genericType = aDecoder.decodeObjectForKey("genericType") as! String
genericPhone = aDecoder.decodeObjectForKey("genericPhone") as! String
orgName = aDecoder.decodeObjectForKey("orgName") as! String

func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(genericCode, forKey: "genericCode")
aCoder.encodeObject(genericName, forKey: "genericName")
aCoder.encodeObject(genericType, forKey: "genericType")
aCoder.encodeObject(genericPhone, forKey: "genericPhone")



It looks like you are not encoding any value for key "orgName" in func encodeWithCoder(aCoder: NSCoder)

When the init(coder aDecoder: NSCoder) is run, it will try to decode a value for key "orgName" which does not exist, since it has been omitted in encodeWithCoder(aCoder: NSCoder). The decoding of this key will result in nil which can not be unwrapped.