我在下面有这个代码,在运行时我想检查并查看字段itemName,itemNote等是否为空,如果是,则显示UIAlertController并阻止用户继续。我尝试使用的代码在saveTapped IBAction函数下。目前,在运行时它似乎绕过所有检查并直接进入" if(item!= nil){"检查。
任何见解都将受到赞赏。
min_value
答案 0 :(得分:1)
您应该检查text
的{{1}}属性,而不是textFields
。
试试这个:
nil
如果您想在没有提醒的情况下访问该值,那么您也可以尝试if let name = itemName.text where name.characters.count > 0 {
//show your alert
} else if let note = itemNote.text where note.characters.count > 0 {
//show your alert
} else if let hours = hoursPlayed.text where hours.characters.count > 0 {
//show your alert
} else if let image = imageHolder.image {
//show your alert
}
:
UITextFields
答案 1 :(得分:1)
使用此简单代码检查空字符串
let bananaName = " " //Empty String
let mango = "Mango"
//Make file for this function and call like my example below return Boolean value for check the value
//Function
struct Checker {
static func isEmptyfor(content:String) -> Bool? {
guard content.trimString.isEmpty else {
print("Not Empty")
return true
}
print("Empty")
return false
}
}
// String Extenion here for Validate trim String
extension String {
var trimString:String {
return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
}
}
//***Final Code here for use this Only for check your string
//Call Function here Like This
//##exp 1
if Checker.isEmptyfor(bananaName)! {
print("Not Empty")
//Do something here
}else{
print("Empty")
}
//##exp 2
if Checker.isEmptyfor(mango)! {
print("Not Empty")
//Do something here
}else{
print("Empty")
}