尝试使用Swift 4 iOS - UITextField制作“字数统计”功能。以下代码无法打印在我的UITextField中键入的字符串的.count。Swift 4 iOS - 来自UITextField的字数
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//INPUT BOX - for Word Count!
let INPUT : UITextField = UITextField(frame: CGRect(x: 35, y: 200, width: 350, height:50))
INPUT.placeholder = "Type Words separated with Spaces!"
INPUT.font = UIFont.init(name:"times", size: 22)
INPUT.backgroundColor = UIColor(red:0x00 ,green:0xff, blue:0x00 ,alpha:1)
self.view.addSubview(INPUT)
//variable holding the value of text typed
let strings : String! = INPUT.text
//variable holding the methods for detecting spaces and new lines
let spaces = CharacterSet.whitespacesAndNewlines.union(.punctuationCharacters)
//variable storing the amount of words minus the spaces
let words = strings.components(separatedBy: spaces)
//method to display the word count in the console
print(words.count)
}
}
这个错误被印在控制台: “结果” =>:0
是什么情况?有没有错误或计数为0? – Adarkas2302
此错误正在打印在控制台中: “结果”=>:0 –
ctaylorgraphics
但是,这是正确的..它应该是0在该位置。我试过你的代码并添加了我的答案。然后它打印2. –