2016-10-13 2 views
5

uitextview가 비어 있는지 어떻게 확인합니까?textview가 비어 있는지 신속히 확인하십시오.

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

그에게 빈 텍스트 뷰를 보낼 사용자를 방지하기 위해 또는 뭔가처럼, 또한 공백으로 확인해야이 충분히 :

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 updated – 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 
} 
관련 문제