1 year ago

#377029

test-img

Newbie

Swift decodable - decode json having base64 encoded string

I am trying to decode JSON response that has encoded base64.

Example JSON:

{ "contract": "JVBERi0xLjQKJeLjz9MKNSAwIG9iago8PC9UeXBlL1hPYmplY3QvQ29sb3JTcGFjZS9EZXZpY2V", "isSuccess": true }
  • How can I decode base64 to string?

My code & Model:

struct ruleModel: Codable {
    let contract: String?
    let isSuccess: Bool?
 }


let decoder = JSONDecoder()
let response = try decoder.decode(ruleModel.self, from: result! as! Data)
print(response)

Decoding Error:

dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around line 1, column 0." UserInfo={NSDebugDescription=Invalid value around line 1, column 0., NSJSONSerializationErrorIndex=0})))

ios

swift

base64

decode

codable

0 Answers

Your Answer

Accepted video resources