简体   繁体   English

致命错误:在Swift 3中解开Optional值时意外发现nil

[英]fatal error: unexpectedly found nil while unwrapping an Optional value in Swift 3

this Struct is work in swift 2 这个结构工作迅速2

I have a Swift 3 struct like this. 我有一个这样的Swift 3结构。

let tempContacts =  NSMutableArray()
let arrayOfArray =  NSMutableArray()

I have encode The Person Object in this for loop 我在此for循环中编码了Person对象

    for person in tempContacts as! [Person] {

        let encodedObject: Data = NSKeyedArchiver.archivedData(withRootObject: person) as Data
        arrayOfArray.add(encodedObject)

    }

I have decode the data in this for loop 我已经在此for循环中解码了数据

let tempContacts2 = NSMutableArray()
   for data in arrayOfArray {

        let person: Person = NSKeyedUnarchiver.unarchiveObject(with: data as! Data) as! Person
        tempContacts2.add(person)   

    }

but unarchiveObject is always return nil value 但是unarchiveObject总是返回nil值

First your model class should conform to the NSCoder protocol. 首先,您的模型类应符合NSCoder协议。 The rest is really simple, there's no need to store the archived results for each object in an array, you can pass the initial array directly to NSKeyedArchiver like this : 其余的操作非常简单,无需将每个对象的归档结果存储在数组中,您可以像这样将初始数组直接传递给NSKeyedArchiver:

class Person: NSObject, NSCoding {
    var name = ""

    init(name: String) {
        self.name = name
    }

    // NSCoder
    required convenience init?(coder decoder: NSCoder) {
        guard let name = decoder.decodeObject(forKey: "name") as? String else { return nil }
        self.init(name: name)
    }

    func encode(with coder: NSCoder) {
        coder.encode(self.name, forKey: "name")
    }
}

let tempContacts = [Person(name: "John"), Person(name: "Mary")]

let encodedObjects = NSKeyedArchiver.archivedData(withRootObject: tempContacts)
let decodedObjects = NSKeyedUnarchiver.unarchiveObject(with: encodedObjects)

As a side note : if NSCoder compliance is correctly implemented in your model class, you can of course use your way of archiving/unarchiving individual objects too. 附带说明 :如果在模型类中正确实现了NSCoder合规性,那么您当然也可以使用归档/取消归档单个对象的方式。 So your original code works too, with some minor adjustments: 因此,您的原始代码也可以工作,但需要进行一些小的调整:

for person in tempContacts {
    let encodedObject = NSKeyedArchiver.archivedData(withRootObject: person)
    arrayOfArray.add(encodedObject)
}

var tempContacts2 = [Person]()
for data in arrayOfArray {
    let person: Person = NSKeyedUnarchiver.unarchiveObject(with: data as! Data) as! Person
    tempContacts2.append(person)
} 

Note 2 : if you absolutely wants to use NSMutableArray s that's possible too, just define tempContacts like this: 注意2 :如果您绝对想使用NSMutableArray ,也可以这样定义tempContacts

let tempContacts =  NSMutableArray(array: [Person(name: "John"), Person(name: "Mary")])

The rest is working without changes. 其余的工作没有任何变化。

Note 3 : The reason it used to work in Swift 2 and it's not working anymore in Swift 3 is that the signature for the NSCoder method func encode(with coder:) changed in Swift 3. 注3 :它曾经在Swift 2中工作,而在Swift 3中不再工作的原因是NSCoder方法func encode(with coder:)的签名在Swift 3中已更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 快速致命错误:解开Optional值时意外发现nil - swift fatal error: unexpectedly found nil while unwrapping an Optional value Swift致命错误:解开Optional值时意外发现nil - Swift fatal error: unexpectedly found nil while unwrapping an Optional value SWIFT-致命错误:解开可选值时意外发现nil - SWIFT - fatal error: unexpectedly found nil while unwrapping an Optional value Swift:致命错误:在展开可选值时意外发现nil - Swift : fatal error: unexpectedly found nil while unwrapping an Optional value Swift-致命错误:解开Optional值时意外发现nil - Swift - Fatal error: unexpectedly found nil while unwrapping an Optional values Swift中的可选类型错误:致命错误:解开可选值时意外发现nil - Optional type error in Swift: fatal error: unexpectedly found nil while unwrapping an Optional value Xcode Swift:appDelgate.window! is nil :致命错误:在解开可选值时意外发现 nil - Xcode Swift : appDelgate.window! is nil : Fatal error: Unexpectedly found nil while unwrapping an Optional value 致命错误:在Tableview中展开Optional值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value in Tableview 致命错误:解开可选值(lldb)时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) 致命错误:展开一个可选值(lldb)时意外发现nil - Fatal error: Unexpectedly found nil while unwrapping an Optional value (lldb)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM