天天看點

iOS 數字、字母和字元串的判斷

- (BOOL)hyb_isOnlyLetters {
  NSCharacterSet *letterCharacterset = [[NSCharacterSet letterCharacterSet] invertedSet];
  return ([self rangeOfCharacterFromSet:letterCharacterset].location == NSNotFound);
}

- (BOOL)hyb_isOnlyNumbers {
  NSCharacterSet *numSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
  return ([self rangeOfCharacterFromSet:numSet].location == NSNotFound);
}

- (BOOL)hyb_isOnlyAlphaNumeric {
  NSCharacterSet *numAndLetterCharSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
  return ([self rangeOfCharacterFromSet:numAndLetterCharSet].location == NSNotFound);
}