How to append a character to a string in Swift?

后端 未结 8 1773
长情又很酷
长情又很酷 2021-01-01 10:15

This used to work in Xcode 6: Beta 5. Now I\'m getting a compilation error in Beta 6.

for aCharacter: Character in aString {
    var str: String = \"\"
    v         


        
8条回答
  •  -上瘾入骨i
    2021-01-01 10:28

    append append(c: Character) IS the right method but your code has two other problems.

    The first is that to iterate over the characters of a string you must access the String.characters property.

    The second is that the append method doesn't return anything so you should remove the newStr.

    The code then looks like this:

    for aCharacter : Character in aString.characters {
        var str:String = ""
        str.append(aCharacter)
        // ... do other stuff
    }
    

提交回复
热议问题