In Swift, a let declaration defines a constant value, which means that the value cannot be changed. A computed property is a value that is calculated at runtime based on some logic or other properties.
Here’s what happens when you try to use the “let” keyword to declare a computed property: Swift can’t differentiate between a constant and a calculated value, so it won’t allow you to declare a computed property as “let” because that would make it seem like a constant.
Error in the swift model:
error build: 'let' declarations cannot be computed properties
The solution is to replace let with var for the target property in your struct.
Let’s say the struct is:
struct MyModel: Codable, Identifiable {
let id: String {
self.myId
}
let myId: String
let description: String
}
The solution will be to modify that struct and make it like this:
struct MyModel: Codable, Identifiable {
var id: String {
self.myId
}
let myId: String
let description: String
}
In the above model, we have id property declaration from let to var.
You can also use a stored property, which is a constant value, inside a computed property.
class YourClass {
let someProperty = somePropertyValue
var computedModifiedProperty: Int {
return someProperty + somePropertyValue
}
}
Remember that computed properties are evaluated every time they are accessed, so if you need a value that will not change, use a normal property instead of a computed one.