Swift,在展开可选值时意外发现 nil (NSKeyedUnarchiver)
Swift, unexpectedly found nil while unwrapping an Optional value (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")
}
}
您似乎没有为 func encodeWithCoder(aCoder: NSCoder)
中的键 "orgName" 编码任何值
当 init(coder aDecoder: NSCoder)
为 运行 时,它将尝试解码不存在的键 "orgName" 的值,因为它已在 encodeWithCoder(aCoder: NSCoder)
中被省略。此密钥的解码将导致无法解包的 nil。
我想通过以下方式在缓存中存储一个数组。 但不知何故我收到错误“在展开可选值时意外发现 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")
}
}
您似乎没有为 func encodeWithCoder(aCoder: NSCoder)
当 init(coder aDecoder: NSCoder)
为 运行 时,它将尝试解码不存在的键 "orgName" 的值,因为它已在 encodeWithCoder(aCoder: NSCoder)
中被省略。此密钥的解码将导致无法解包的 nil。