Help with Deleting Characters in a String (Code not Working)

I'm working on a problem that requires me to remove the first character in a string a given number of times.The code below is not the code that the lesson requires. But I wrote it in trying to flesh out my thoughts, and I would still like to know why the below code does not work.

var newString = "Hello"

if newString.count != 0 {
    for eachPass in 0...4 {
        newString -= newString.first

When I try to compile, I get the following error messages:

Binary operator '-=' cannot be applied to operands of type 'String' and 'String.Element?' (aka 'Optional')

Cannot convert value of type 'String.Element?' (aka 'Optional') to expected argument type 'String'

Could someone please let me know why this code is not working and what exactly these error messages refer to? I'm a beginner, so a beginner friendly explanation would be greatly appreciated. Thanks so much.

Simply put, Swift does not define operator -= between String and character. Think about it, one can reasonable expect -= to remove the matching prefix, suffix, or even all occurrences of the said character. I personally think the last one would make the most sense.

Anyhow, if you want to remove the first character, use removeFirst, or removeFirst(_:) instead.

Now, nothing stops you from defining your own -= between two String, though I wouldn't recommend it. To see how you can add new operators (for future reference), see Advance Operators


Thank you Lantua, not sure how I forgot that.