2016-10-13 10 views
5

मुझे आश्चर्य है कि अगर मैं एक यूटक्स्टव्यू खाली है तो मैं उचित जांच कैसे करूं।स्विफ्ट चेक यदि टेक्स्टव्यू खाली है

if let descText = myTextView.text { 
    if descText.isEmpty { 
     descErrorLabel.isHidden = false 
    } else { 
     descErrorLabel.isHidden = true 
    } 
} 

यह यह पर्याप्त एक खाली TextView भेजने से उपयोगकर्ता को रोकने के लिए या मैं कुछ की तरह भी व्हाइटस्पेस की जांच करना चाहिए,:

stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).isEmpty 

अभी मैं एक मान्यता fucntion जो जांच करता है

+0

बाद में कोई अच्छा है। –

+0

http://stackoverflow.com/questions/33036458/how-to-check-uitextfield-is-not-blank-in-swift-2-0-is-there-better-way-to-count/33036724 –

+0

@ EricAya – user2636197

उत्तर

6

आप यह सब रोल कर सकता है कुछ इस तरह में ...

func validate(textView textView: UITextView) -> Bool { 
    guard let text = textView.text, 
     !text.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).isEmpty else { 
     // this will be reached if the text is nil (unlikely) 
     // or if the text only contains white spaces 
     // or no text at all 
     return false 
    } 

    return true 
} 

फिर आप किसी भी UITextView को सत्यापित कर सकते हैं ...

if validate(textView: textView) { 
    // do something 
} else { 
    // do something else 
} 
संबंधित मुद्दे