我正在研究textViewDidChange中的字数功能。只要我输入它崩溃。当我粘贴一个句子时,调试器显示它获得了单词计数,但是在将计数添加到带有致命错误的按钮标题时它崩溃了:意外地在解包可选值时发现了零。斯威夫特字数致命错误
func textViewDidChange(textView: UITextView) {
let wordCount = textView.text.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).count
let words = String(format: "%d", wordCount)
countButton.title = "\(words)"
}
检查您的countButton IBOutlet已正确连接。 – Moritz
最后一行代码行中的count是多少? – vadian
这是一个错字,改为单词。 countButton是以编程方式创建的。 – mrcrowley