I am trying to make a Data-Storage using NSCoder, for some weird reason, its showing this error to me where i try to use the .encode
keyword, please help me understand what i'm doing wrong..
let encoder = PropertyListEncoder()
do {
let data = try encoder.encode(self.itemArray) // <--- showing error here
} catch {
}
Never-mind, I found the problem! If you guys are facing the same problem where you make your array takes data specified in a class, you need to make the class 'Encodable' ie
import Foundation
class CellItemReg : Encodable { // <-- 'Encodable'
var done : Bool = false
var title : String = ""
}
This fixed for me in Swift iOS.
Inherit Codable in the class which you are trying to encode.
In your case,
let encoder = PropertyListEncoder()
do {
let data = try encoder.encode(self.itemArray) // <--- showing error here
} catch {
}
Let's assume itemArray is the array of a class named 'Item'. Then your 'Item' needs to inherit Codable in swift.
Just as below.
import Foundation
class Item: Codable {
var id: Int!
}
All the best!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With