Yash Kothari
Yash Kothari

Reputation: 243

Generic parameter 'Value' could not be inferred

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

Upvotes: 5

Views: 5107

Answers (2)

Rahul Kavungal
Rahul Kavungal

Reputation: 91

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!

Upvotes: 1

Yash Kothari
Yash Kothari

Reputation: 243

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

Upvotes: 7

Related Questions