-3

I have a problem with an error described in the title - "Cannot assign to property: 'desc' is a 'let' constant".

I would like to assign a string variable to 'desc' in a JSON file. JSON was previously downloaded to a variable named "result".

I have followed by answers for similar questions, but I can't apply resolution to my code.

Class:

import UIKit

class informationViewController: UIViewController {

    //values received from previous view
    var semesterNumber:String?
    var dayNumber:String?
    var text_desc:String?
    var result:Data1!
    var numberRow:Int?

Function:

func saveToJsonFile(result2 : inout Data1) {

        result?.data[Int(semesterNumber!)!].monday[numberRow!].desc = "cos" //Error: Cannot assign to property: 'desc' is a 'let' constant
        
        //Coninue of exectution....
        

Function call:

 @IBAction func isSaveClicked(_ sender: Any) {
        saveToJsonFile(result2 : &result!)
    }

Errors messages

MatoNator
  • 1
  • 1
  • 4
  • 3
    Well, I think you need to start with the fact that `func saveToJsonFile(result?.data[Int(semesterNumber!)!].monday[numberRow!].desc: inout Data1)` is total nonsense. A function _declaration_ is totally different from a function _call_. You seem to be confusing them. – matt Feb 21 '22 at 23:41
  • 3
    Matt has a point. What you posted is not a valid function declaration. It is an odd (and invalid) mix of a function declaration and a function call. You should study the syntax of function declarations. – Duncan C Feb 22 '22 at 01:38
  • 1
    [Functions](https://docs.swift.org/swift-book/LanguageGuide/Functions.html) chapter in the Swift Programming Language book – Joakim Danielson Feb 22 '22 at 07:50
  • I have fixed the declaration and call of the function with success. The last problem is a message: "Cannot assign to property: 'desc' is a 'let' constant". Why does the problem still occurs except that I call and declare the function like [there](https://www.hackingwithswift.com/example-code/language/what-are-inout-parameters) – MatoNator Feb 22 '22 at 22:34

1 Answers1

0
    class infoViewController: UIViewController {
        
        var result:Data1!
        
        override func viewDidLoad() {
            saveToJsonFile(result2 : &result!)
        }
        
        func saveToJsonFile(result2 : inout Data1) {
            result2.data[1].monday[1].desc = "cos"
        }
    }
    
    struct Data1{
        var data: [Monday]
    }
    struct Monday {
        var monday: [Desc]
    }
    struct Desc{
        let desc: String
    }

If you try as above you will get "Cannot assign to property: 'desc' is a 'let' constant" error. So you need to change the let into var, because let is immutable.

class infoViewController: UIViewController {
            
            var result:Data1!
            
            override func viewDidLoad() {
                saveToJsonFile(result2 : &result!)
            }
            
            func saveToJsonFile(result2 : inout Data1) {
                result2.data[1].monday[1].desc = "cos"
            }
        }
        
        
        struct Data1{
            var data: [Monday]
        }
        struct Monday {
            var monday: [Desc]
           
        }
        struct Desc{
            var desc: String
        }
Dinesh
  • 167
  • 2
  • 11