我一直在用Swift 3更新一些舊代碼和答案,但是當(dāng)我接觸Swift Strings和Indexing時,很難理解。具體來說,我正在嘗試以下操作:let str = "Hello, playground"let prefixRange = str.startIndex..<str.startIndex.advancedBy(5) // error第二行給我以下錯誤“ advancedBy”不可用:要使索引前進(jìn)n個步驟,請在生成索引的CharacterView實(shí)例上調(diào)用“ index(_:offsetBy :)”。我看到String有以下方法。str.index(after: String.Index)str.index(before: String.Index)str.index(String.Index, offsetBy: String.IndexDistance)str.index(String.Index, offsetBy: String.IndexDistance, limitedBy: String.Index)剛開始這些讓我很困惑,所以我開始和他們一起玩直到我理解它們。我在下面添加一個答案,以顯示它們的用法。
3 回答

蝴蝶刀刀
TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個贊
在tableViewController內(nèi)部創(chuàng)建一個UITextView。我使用了函數(shù):textViewDidChange,然后檢查了返回鍵輸入。如果檢測到返回鍵輸入,則刪除返回鍵的輸入并關(guān)閉鍵盤。
func textViewDidChange(_ textView: UITextView) {
tableView.beginUpdates()
if textView.text.contains("\n"){
textView.text.remove(at: textView.text.index(before: textView.text.endIndex))
textView.resignFirstResponder()
}
tableView.endUpdates()
}
- 3 回答
- 0 關(guān)注
- 1820 瀏覽
添加回答
舉報
0/150
提交
取消