0

how can i change an item in an array (Cannot convert value of type 'Int' to expected argument type 'String')

var headersSend: HTTPHeaders = [
            "changeMe": "none",
            "Accept": "application/json"
        ]
  
  headersSend[0] = "changeMe" : "Changed!" // <--- ? Cannot convert value of type 'Int' to expected argument type 'String'
1
  • 2
    That's a dictionary, not an array. I'd tend to say headersSend["changeMe"] = "Changed"] Commented Nov 19, 2020 at 13:09

1 Answer 1

1

headersSend is a dictionary, not an array. It stores key-value pairs, where key and value are both of type String.

Accessing through headersSend[0] means you want to change item at key 0. You have a compilation error because dictionary's key is not of type Int. You have to use String key instead.

That's the way we change value at specific key

dictionary[key] = newValue

In your case it would be

headersSend["changeMe"] = "Changed!"


I suggest you to read more about collection types here.

Sign up to request clarification or add additional context in comments.

1 Comment

//It stores key-value pairs .. thank you +++++ 👍🏻

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.