Error using += to append a Character to a String:

2019-07-04 11:02发布

问题:

When trying to use the += operator to append a Character to a String, I'm getting the following error:

String is not identical to UInt8

The error occurs on the puzzleOutput += char line in the code below:

let puzzleInput = "great minds think alike "
var puzzleOutput = ""

for char in puzzleInput
{
    switch char
    {
    case "a","e","i","o","u":
        continue

    default:
       puzzleOutput += char
    }
}

println(puzzleOutput)

How can I append a Charater to a String?

回答1:

The use of += to append a Character to a String was intentionally deleted from the language several betas ago. Use

puzzleOutput.append(char)

instead.



标签: swift ios8