I am writing some requests that I need to convert json string as input to some type of Codable
as payload. however, I have the error shows cannot assign to property: 'payload' is a 'let'
under the function. why is it?
Error:
Line 7: Char 13: error: cannot assign to property: 'payload' is a 'let' constant in solution.swift
payload.first = "second first"
~~~~~~~ ^
Line 8: Char 13: error: cannot assign to property: 'payload' is a 'let' constant in solution.swift
payload.last = "second last"
~~~~~~~ ^
Code:
struct Name: Codable {
var first: String
var last: String
}
func session(payload: Name) {
payload.first = "second first"
payload.last = "second last"
print(payload)
}
let json = "{"first": "i am first", "last": "i am last"}"
let data = try json.data(using: .utf8)!
var payload = try JSONDecoder().decode(Name.self, from: data)
session(payload: payload)