BOOL hasLeadingNumberInString(NSString* s) { if (s) return [s length] && isnumber([s characterAtIndex:0]); else return NO;
}
If you process several NSStrings at once (for example, loop through an array), and you want to check each of them for formatting, for example, leading numbers, it is better to enable checks so that you do not try to evaluate an empty or nonexistent string.
Example:
NSString* s = nil; //Edit: s needs to be initialized, at the very least, to nil. hasLeadingNumberInString(s); //returns NO hasLeadingNumberInString(@""); //returns NO hasLeadingNumberInString(@"0123abc"); //returns YES
Beljoda
source share