Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic parameter 'Value' could not be inferred

Tags:

xcode

swift4

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 {   
}
like image 992
Yash Kothari Avatar asked Sep 10 '18 03:09

Yash Kothari


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 = ""
}
like image 169
Yash Kothari Avatar answered Oct 14 '22 03:10

Yash Kothari


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!

like image 21
Rahul Kavungal Avatar answered Oct 14 '22 02:10

Rahul Kavungal