将字符附加到Swift中。

时间:2023-01-05 18:46:54

I'm trying to append Character to String using "+=", but It doesn't really work. Once I tried with append method, it works. I just wonder why it is. The compiler says "string is not identical to Unit8".

我试着用“+=”来将字符附加到字符串中,但它并没有真正起作用。一旦我尝试了append方法,它就可以工作了。我只是想知道为什么。编译器说“string与Unit8不一样”。

let puzzleInput = "great minds think alike"
var puzzleOutput = " "
for character in puzzleInput {

    switch character {
        case "a", "e", "i", "o", "u", " ":
        continue

    default:
        // error : doesn't work
        puzzleOutput += character
        //puzzleOutput.append(character)
    }
}
println(puzzleOutput)

3 个解决方案

#1


3  

20140818, Apple updated:

20140818,苹果更新:

Updated the Concatenating Strings and Characters section to reflect the fact that String and Character values can no longer be combined with the addition operator (+) or addition assignment operator (+=). These operators are now used only with String values. Use the String type’s append method to append a single Character value onto the end of a string.

更新连接字符串和字符部分,以反映字符串和字符值不能再与加法运算符(+)或加法赋值运算符(+=)相结合的事实。这些操作符现在只使用字符串值。使用字符串类型的append方法将单个字符值附加到字符串的末尾。

Document Revision History 2014-08-18

文件修订历史2014-08-18

#2


0  

To append a Character to a String in Swift you can do something similar to the following:

要在Swift中附加一个字符,您可以执行以下操作:

var myString: String = "ab"
let myCharacter: Character = "c"
let myStringChar: String = "d"

myString += String(myCharacter) // abc
myString += myStringChar // abcd

#3


0  

Updated version

更新版本

let puzzleInput = "great minds think alike"
var puzzleOutput = ""
for character in puzzleInput.characters {
  switch character {
  case "a", "e", "i", "o", "u", " ":
   continue
  default:
    puzzleOutput += String(character)
  }
}
print(puzzleOutput)
// prints "grtmndsthnklk"

#1


3  

20140818, Apple updated:

20140818,苹果更新:

Updated the Concatenating Strings and Characters section to reflect the fact that String and Character values can no longer be combined with the addition operator (+) or addition assignment operator (+=). These operators are now used only with String values. Use the String type’s append method to append a single Character value onto the end of a string.

更新连接字符串和字符部分,以反映字符串和字符值不能再与加法运算符(+)或加法赋值运算符(+=)相结合的事实。这些操作符现在只使用字符串值。使用字符串类型的append方法将单个字符值附加到字符串的末尾。

Document Revision History 2014-08-18

文件修订历史2014-08-18

#2


0  

To append a Character to a String in Swift you can do something similar to the following:

要在Swift中附加一个字符,您可以执行以下操作:

var myString: String = "ab"
let myCharacter: Character = "c"
let myStringChar: String = "d"

myString += String(myCharacter) // abc
myString += myStringChar // abcd

#3


0  

Updated version

更新版本

let puzzleInput = "great minds think alike"
var puzzleOutput = ""
for character in puzzleInput.characters {
  switch character {
  case "a", "e", "i", "o", "u", " ":
   continue
  default:
    puzzleOutput += String(character)
  }
}
print(puzzleOutput)
// prints "grtmndsthnklk"

相关文章