0

I understand the problem, but I don't know how to solve it, I found other thread, but they are very specific problem. I have my for var:

    static var DeliveryStatusArray =
    [
        "claimable": [],
        "onTime": [],
        "future": [],
        "claimDone": [],
        "tooOld": [],
    ]

My function is :

    static func moreCard(status:String, newJson: JSON)
{
    var json = JSON(DeliveryStatusArray[status] as Any)
    var tmpArray = json[0]["cards"].arrayValue

    newJson["cards"].array?.insert(tmpArray, at: 0)
    DashboardManager.DeliveryStatusArray[status]?.removeAll()
    DashboardManager.DeliveryStatusArray[status] = tmpArray
}

In this function I want to insert at the begining of newJson["cards"] tmpArray. In second time I need to pass newJson at DeliveryStatusArray["status"]. I got my error on the line

newJson["cards"].array?.insert(tmpArray, at: 0)

In my view, the prototype of my function is:DashboardManager.moreCard(status: section, newJson: json["data"])

1 Answer 1

1

Swift only allows constants as the method parameter. In your case to fix the problem, you can use the inout parameter in the method as below.

  static func moreCard(status:String, newJson: inout JSON)
  {
     var json = JSON(DeliveryStatusArray[status] as Any)
     var tmpArray = json[0]["cards"].arrayValue

     newJson["cards"].array?.insert(tmpArray, at: 0)
     DashboardManager.DeliveryStatusArray[status]?.removeAll()
     DashboardManager.DeliveryStatusArray[status] = tmpArray
  }
Sign up to request clarification or add additional context in comments.

2 Comments

Do I need to change my prototype ? When I change it to DashboardManager.moreCard(status: section, newJson: json["data"]) I still have the same error, and if I don't change the prototype, I have the error Passing value of type 'Json' to an inout parameter requires explicit &
No just pass pre-created prototype object in the inout parameter and you will be able to mutate that object in your method. Parameter to be passed in the method will be DashboardManager.moreCard(status: section, newJson: &json["data"]) or var jsondata = json["data"] then DashboardManager.moreCard(status: section, newJson: &jsondata)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.