guard let text = txtField.text, !text.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).isEmpty else{
let alertController = UIAlertController(title: "New alert!", message:"Please Enter Email Id", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))
self.present(alertController, animated: true, completion: nil)
return
}
如果我使用上面的代码,我只删除textfield中的前导空格。我想删除文本字段文本中包含的空格。
使用以下扩展字符串
extension String
{
func isNumber() -> Bool{
let numberCharacters = CharacterSet.decimalDigits.inverted
return !self.isEmpty && self.rangeOfCharacter(from:numberCharacters) == nil
}
struct NumberFormat
{
static let instance = NumberFormatter()
}
var integerValue:Int?
{
if self.isEmpty
{
return 0
}
else
{
return (NumberFormat.instance.number(from: self)?.intValue) ?? 0
}
}
}