How to check UITextField is not blank in Swift 2.0? Is there better way to count the string? [duplicate]

青春壹個敷衍的年華 提交于 2019-12-25 06:04:07

问题


I am try to check textfield is blank or not and what I am trying is

if(EmailTextField.text.characters.count>0)
{
}  

but I can't use relation operator on this count method. Why?


回答1:


By extension... To ensure that its not even take any blank spaces...

extension String {

var isBlank: Bool {
       get {
            let trimmed = stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
            return trimmed.isEmpty
        }
   }
}

Usage:

if !EmailTextField.text!.isBlank{
   //TextField is not blank
}



回答2:


You can use the String's isEmpty property:

//if EmailTextField is a UITextField instance 
if EmailTextField.text != nil && !EmailTextField.text!.isEmpty {

}

if let text = EmailTextField.text where !text.isEmpty {

}


来源:https://stackoverflow.com/questions/33036458/how-to-check-uitextfield-is-not-blank-in-swift-2-0-is-there-better-way-to-count

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!