Ios 如何检查TextView是否为空?
我用这段代码检查Textview是否为空。若我给空间,它可以正常工作,但若我按下“回车”按钮,它允许我在数据库中输入数据。这有什么问题Ios 如何检查TextView是否为空?,ios,swift,validation,uitextview,Ios,Swift,Validation,Uitextview,我用这段代码检查Textview是否为空。若我给空间,它可以正常工作,但若我按下“回车”按钮,它允许我在数据库中输入数据。这有什么问题 func validation()->Int { var i = 1 //1st way if txt_view.text.isEmpty { var alert = UIAlertView() alert.title = "Message" alert.message =
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
}
更改第二次尝试包括新行:
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
,,谢谢。。这成功了!!