我想连接字符串,我这样做:如何连接可选的字符串在SWIFT
var text: String!
....
text = "hello"
text += "! How r you?"
但我得到了以下错误:
cannot convert value of type 'String!' to expected argument type 'inout String' text += "!" ^~~~
我怎样才能解决呢?谢谢
我想连接字符串,我这样做:如何连接可选的字符串在SWIFT
var text: String!
....
text = "hello"
text += "! How r you?"
但我得到了以下错误:
cannot convert value of type 'String!' to expected argument type 'inout String' text += "!" ^~~~
我怎样才能解决呢?谢谢
你必须定义变量没有感叹号,像:
var text: String = ""
text = "hello"
text += "! How r you?"
或更短的背景下的功能:
var text: String = "hello"
text += "! How r you?"
你可以这样做得到它
var text = "Hello!" as! String
text = text + " How are you?"
希望它hel PS。
在处理Swift中的感叹号时,您必须记住尝试使用!
访问不存在的可选值会触发运行时错误。
如果可选项有一个值,则认为它是“不等于零”。所以,你必须检查这一点,你才能获取其潜在价值:
if text != nil {
text += "! How are you?"
}
变化'VAR文本:字符串''到VAR文本=“”' – Johnykutty