2016-05-15 17 views
2

我必須從UILabel中刪除一個條目。我這樣做是通過將其文本複製到一個字符串變量並刪除字符串的結束索引。 UILabel包含一個值,但它被複制到的字符串不包含任何內容。非常感激任何的幫助。無法從Swift中的UILabel複製文本

var displayString = display.text! 
displayString.removeAtIndex(displayString.endIndex) //error at this line. 
display.text! = displayString 

字符串的狀態可以在此屏幕截圖中看到。 enter image description here

和調試器可以在這裏看到,顯示UILabel實際上確實包含字符串。 enter image description here

回答

1

您不應該刪除endIndex,而是在結束之前。

if displayString != "" { 
    displayString.removeAtIndex(displayString.endIndex.predecessor()) 
} 
+0

.predecessor()像變魔術一樣工作:檢查空字符串刪除字符前 - 如果確實是值得刪除您應該只從字符串中刪除的東西!非常感謝! –

+0

Upvoted。我試圖接受答案,但它不會讓我。說它必須在回答的5分鐘內完成。爲此道歉。 –

+0

做到了。天哪,Stackoverflow今天在玩我的腦海。你從我身上獲得了第二套謝意。非常感謝。 –