import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
let stringArray = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
let even = stringArray.map { Int($0)!
}.filter {
$0 % 2 == 0
}
print(even)
}
}
extension UIViewController: UITextFieldDelegate {
//在每次用戶按下鍵盤,字母在屏幕上顯示出來之前被調用
public func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// 1. 每次的輸入轉換成一個整數(shù)
if let n = Int(string) {
// 2. 判斷是否能夠被2整除
if n % 2 == 0 {
print(n)
}
}
return true
}
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者