Mika
Mika

Reputation: 67

Update specific value in structure

I can't seem to find out how to update a specific value in a structure in Swift 4. I have this structure:

struct Export: Decodable {
    let id: String
    let name: String
    let exportType: String
}

It is filled with values I got from a JSON.
I was using JSONDecoder

self.Exp = try JSONDecoder().decode([Export].self, from: data!)

Now I'm receiving a new JSON only containing an id.
I want to update the id of this structure with the new value.
The JSON sends a response like that :

{
    "id": "70CD044D290945BF82F13C13B183F669"
}

So even when I try to save it in a separate structure I get this error

dataCorrupted(Swift.DecodingError.Context(codingPath: [], 
debugDescription: "The given data was not valid JSON.", 
underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 
"JSON text did not start with array or object and option to allow fragments not set."
UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.})))

I tried to look for solutions before posting but I couldn't find any I'm pretty new to JSON handling and Swift...

Upvotes: 1

Views: 1635

Answers (2)

Ankit Jayaswal
Ankit Jayaswal

Reputation: 5679

First, of all your JSON is not in correct format.

Secondly, after you will receive correct JSON, for self.Exp you are getting array, but for your idDict you have only one dictionary object.

So, Keep those property optional, which are not necessary to appear in JSON. In your case it would be name and exportType as:

struct Export: Decodable {
    var id: String
    var name: String?
    var exportType: String?
}

It can be used for self.Exp as:

self.Exp = try JSONDecoder().decode([Export].self, from: data!)

and for idDict as:,

idDict = try JSONDecoder().decode(Export.self, from: data!)

Upvotes: 0

R.B.
R.B.

Reputation: 423

Leaving the JSON part aside, you won't be able to update id in Export, since it is a let constant. You may want to change it to var.

If I understand correctly, you are receiving a JSON response with only an id. You don't create an Export struct out of it. You would need to process this JSON response separately to grab the id you are looking for. Like this:

import Foundation

let jsonText = """
{"id": "70CD044D290945BF82F13C13B183F669"}
"""

struct IdResponse: Codable {
    let id: String
}

let idResponse: IdResponse = try! JSONDecoder().decode(IdResponse.self, from: jsonText.data(using: .utf8)!)

And finally, update your Export struct:

import Foundation

struct Export: Decodable {
    var id: String
    let name: String
    let exportType: String
}

// Build export object
var export: Export = Export(id: "1", name: "Name", exportType: "TypeA")

// Grab JSON response from somewhere, which contains an updated id
let idResponse: IdResponse = try! JSONDecoder().decode(IdResponse.self, from: jsonText.data(using: .utf8)!)

// Update the object
export.id = idResponse.id

Upvotes: 1

Related Questions