所以我目前正在阅读 Apple 的新编程语言 Swift 文档/指南。
在“Concatenating Strings and Characters”的标题下,它表示可以将字符串和字符连接起来形成一个新的字符串,并提供了一个示例。
let string1 = " there"
let character1: Character = "!"
let characterPlusString = character1 + string1
但是当我执行这个时,所有连接字符串和字符的示例都会抛出错误:
<EXPR>:15:38: error: cannot invoke '+' with an argument list of type '(Character, String)
我在 XCode 版本 6.0.1 (6A317) 中使用 playground
我已经做了一些研究,但到目前为止一无所获。语言更新了吗? (这本书不是)感谢所有答案,如果这个问题已经被问到但我没有找到,我很抱歉。
最佳答案
根据这个链接
您可以使用追加函数添加字符串和字符
var helloString = "hello"
let exclamationMark: Character = "!"
helloString.append(exclamationMark)
helloString//prints "hello!"
如果你必须使用“+”运算符,你将不得不对你的角色进行类型转换
helloString = helloString + String(exclamationMark)
helloString//prints "hello!"
希望对您有所帮助!
关于swift - 为什么 "The Swift Programming Language"说可以将一个字符和一个字符串连接到一个新字符串,而我的 playground 不允许这样做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26399681/