Swift,在展开Optional值时意外地发现了nil(NSKeyedUnarchiver)

时间:2016-05-17 10:30:08

标签: swift nsuserdefaults nskeyedunarchiver

我想通过以下方式将数组存储在缓存中。 但不知何故,我收到错误" 意外发现nil,同时解开一个可选值"

我的代码

从缓存加载

  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")
            }
        }
        //}
    }
}

保存到缓存

 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
        {
            testArray.removeFirst()
        }
        else{
            testArray.append(generic)
        }
        readArray = testArray
    }
    else{
        readArray.append(generic)
    }


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

有人可以帮我解决一些问题吗? 使用NSCoding协议进行编辑

 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")
}

}

1 个答案:

答案 0 :(得分:3)

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.