Friday, June 14, 2013

NSCharacterSet isSupersetOfSet example in Objective C (iOS).


NSCharacterSet isSupersetOfSet

Returns a Boolean value that indicates whether the receiver is a superset of another given character set.

- (BOOL)isSupersetOfSet:(NSCharacterSet *)theOtherSet

Parameters of [NSCharacterSet isSupersetOfSet]
theOtherSet
A character set.

Return Value of [NSCharacterSet isSupersetOfSet]
YES if the receiver is a superset of theOtherSet, otherwise NO.

NSCharacterSet isSupersetOfSet example.
#pragma mark - UItextfield Delegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([string isEqualToString:@"("]||[string isEqualToString:@")"]) {
        return TRUE;
    }

    NSLog(@"Range ==%d  ,%d",range.length,range.location);
    //NSRange *CURRANGE = [NSString  rangeOfString:string];

    if (range.location == 0 && range.length == 0) {
        if ([string isEqualToString:@"+"]) {
            return TRUE;
        }
    }
    return [self isNumeric:string];
}

-(BOOL)isNumeric:(NSString*)inputString{
    BOOL isValid = NO;
    NSCharacterSet *alphaNumbersSet = [NSCharacterSet decimalDigitCharacterSet];
    NSCharacterSet *stringSet = [NSCharacterSet characterSetWithCharactersInString:inputString];
    isValid = [alphaNumbersSet isSupersetOfSet:stringSet];
    return isValid;
}

Example of [NSCharacterSet isSupersetOfSet].
BOOL valid;
NSCharacterSet *alphaNums = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *inStringSet = [NSCharacterSet characterSetWithCharactersInString:myInputField.text];
valid = [alphaNums isSupersetOfSet:inStringSet];   
if (!valid) // Not numeric

NSCharacterSet isSupersetOfSet example.
NSRegularExpression *numEx = [NSRegularExpression
    regularExpressionWithPattern:@"^[-+]?[0-9]+$" options:0 error:nil
];
NSLog(@"%ld", [numEx numberOfMatchesInString:@"-200" options:0 range:NSMakeRange(0, 4)]);
NSLog(@"%ld", [numEx numberOfMatchesInString:@"001A" options:0 range:NSMakeRange(0, 4)]);
For character set, use [NSCharacterSet decimalDigitCharacterSet].

BOOL isNum = [[NSCharacterSet decimalDigitCharacterSet]
    isSupersetOfSet:[NSCharacterSet characterSetWithCharactersInString:@"001AA"]
];

End of NSCharacterSet isSupersetOfSet example article.