我已使用此代码检查Textview是否为空。如果我给空间,它的工作正常,但是如果我按下"输入"按钮它允许我在数据库中输入我的数据。这有什么问题?
func validation()->Int
{
var i = 1
//1st way
if txt_view.text.isEmpty
{
var alert = UIAlertView()
alert.title = "Message"
alert.message = "Write Career Objective"
alert.addButtonWithTitle("OK")
alert.show()
i = 0
}
//2nd way
if txt_view.text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) == ""
{
var alert = UIAlertView()
alert.title = "Message"
alert.message = "Write Career Objective"
alert.addButtonWithTitle("OK")
alert.show()
i = 0
}
return i
}
答案 0 :(得分:5)
更改您的第二次尝试以包含新行:
if txt_view.text.stringByTrimmingCharactersInSet(
NSCharacterSet.whitespaceAndNewlineCharacterSet()).isEmpty
{
var alert = UIAlertView()
alert.title = "Message"
alert.message = "Write Career Objective"
alert.addButtonWithTitle("OK")
alert.show()
i = 0
}
另外,与空字符串isEmpty
相比,我总是更喜欢== ""
。