To be absolutely sure that you have tested for a blank value, you should also test for an empty string.
This construct does it, and leads to less typing than using isEqualToString:
, but it is largely a question of personal preference:
if ( content.text == nil || content.text.length == 0 ) {
}
People with a more intimate knowledge of the Objective-C runtime might point out that testing for nil
is redundant, as sending a message to nil
will always yield nil (or 0), so content.text.length
is nil if content.text
is. Also, if your surroundings are populated with hard-core C coders, they could similarly point out that testing for 0 in an if statement is for wimps.
Hence, the typing can be reduced to the following, at the cost of some clarity of your code:
if ( !content.text.length ) {
}
As for a regex to test an email address, there is a good discussion here, and the regex in Cocoa can be done like this:
NSPredicate *regexPred = [NSPredicate predicateWithFormat: @"self matches[cd] '<email regex here>'"];
BOOL match = [regexPred evaluateWithObject: content.text];