I am facing similar issue....
Error:
underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}
Question
I know that using using JSONDecoder we can convert the downloaded JSON data into class/struct object.
Is there a way to do the same for raw data(NSData)/octect. Since downloaded is not a json, I am getting error.
I have class like this
Code
public struct FileData: Codable{ public var data: Data?
public init (data: Data? = nil){ self.data = data }
}
Is there a way to assign the downloaded data to FileData().data via decoding
extension Data {
func decode() -> Result<T, Error> where T: Codable {
do {
let decoder = JSONDecoder()
let object: T = try decoder.decode(T.self, from: self)
return Result.success(object)
} catch {
return Result.failure(error)
}
}
Expectation:
Here T is FileData.
I using REST Get api to download(octet stream) a file data and want to set it to FileData().data. It seems like raw data can't be Codable? Is that true? If so, I think I need to manually assign the downloaded data to FileData().data