How to append a character to a string in Swift?

后端 未结 8 1772
长情又很酷
长情又很酷 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:37

    According to Swift 4 Documentation , You can append a Character value to a String variable with the String type’s append() method:

    var welcome = "hello there"
    
    let exclamationMark: Character = "!"
    welcome.append(exclamationMark)
    // welcome now equals "hello there!"
    

提交回复
热议问题