3

I have a array:

[h,e,l,l,o]

And i want to replace an value on the index, for example i want to change the first l at index 2. I know the index so that is not a problem, the only problem is to replace that value.

How can i do this?

1
  • What did you tried? Post your approach here and we can give answer you. Commented Aug 8, 2015 at 11:43

2 Answers 2

14

the insert function will add a new value to the array, what you need to do is simply

var newCharacter = "a"

arr[2] = newCharacter
Sign up to request clarification or add additional context in comments.

Comments

1

As mentionned here https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html

You can modify your array like that:

var array = ["h", "e", "l", "l", "o"]
array[2] = "Six eggs"

Comments

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.