Generic parameter 'Value' could not be inferred

Viewed 3627

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 {   
}
2 Answers

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!

Related