How to append a character to a string in Swift?

后端 未结 8 1841
长情又很酷
长情又很酷 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条回答
  •  太阳男子
    2021-01-01 10:21

    Update for the moving target that is Swift:

    Swift no longer has a + operator that can take a String and an array of characters. (There is a string method appendContentsOf() that can be used for this purpose).

    The best way of doing this now is Martin R’s answer in a comment below:

    var newStr:String = str + String(aCharacter)
    

    Original answer: This changed in Beta 6. Check the release notes.I'm still downloading it, but try using:

    var newStr:String = str + [aCharacter]
    

提交回复
热议问题