我想連接字符串,我這樣做:如何連接可選的字符串在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