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)
}
}
このエラーがコンソールに印刷されている:あなたのTextViewには、その時点で、それには内容がないためです0
何が起こるのでしょうか?そこにエラーがあるか、またはカウントが0ですか? – Adarkas2302
このエラーはコンソールに出力されています: "Result" =>:0 –
ctaylorgraphics
これは正しいです。私はあなたのコードを試して、私の答えを追加しました。その後、2を表示します。 –