Sunday 20 December 2015

String Utilities

Let's have a look into for String categories with below features,

* Email validation
* Phone number validation
* String empty validation
* Remove number characterset

and more on..

  1. Create a New file in XCode. File -> New -> File (Cmd + N)
  2. Choose "Cocoa Touch Class" 
  3. Create the file name as "NSString+Utilities" and subclass it with NSString
  4. Then, click finish. Your category file is ready.
We are gonna see basic String category methods which are listed below


- (NSString *)removeNumbersFromString;
- (BOOL)isEmailValid;
- (BOOL)isMobileValid;
- (BOOL)validateString;

Let's have a look into "NSString+Utilities.h"

#import <Foundation/Foundation.h>

@interface NSString (StringValidations)

- (NSString *)removeNumbersFromString;
- (BOOL)isEmailValid;
- (BOOL)isMobileValid;
- (BOOL)validateString;


@end

And, your "NSString+Utilities.m"

#import "NSString+Utilities.h"

@implementation NSString (StringValidations)

- (NSString *)removeNumbersFromString
{
    NSString *trimmedString = nil;
    NSCharacterSet *numbersSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    trimmedString = [self stringByTrimmingCharactersInSet:numbersSet];
    return trimmedString;
}

/**
 * Method to validate the email
 * @param email variable from NSString class type
 * @return email returns BOOL result
 */
- (BOOL)isEmailValid {
    NSString *emailRegex = @"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}+\\@[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}+\\.[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:self];
}

/**
 * Method to validate the email
 * @param number variable from NSString class type
 * @return number returns BOOL result
 */
- (BOOL)isMobileValid {
    NSError *error = NULL;
    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:&error];
    
    NSRange inputRange = NSMakeRange(0, [self length]);
    NSArray *matches = [detector matchesInString:self options:0 range:inputRange];
    
    // no match at all
    if ([matches count] == 0) {
        return NO;
    }
    // found match but we need to check if it matched the whole string
    NSTextCheckingResult *result = (NSTextCheckingResult *)[matches objectAtIndex:0];
    
    if ([result resultType] == NSTextCheckingTypePhoneNumber && result.range.location == inputRange.location && result.range.length == inputRange.length) {
        // it matched the whole string
        return YES;
    }
    else {
        // it only matched partial string
        return NO;
    }
}

- (BOOL)validateString {
    id stringValue = self;
    if ([stringValue length] == 0 && stringValue == [NSNull null] && stringValue == nil)
        return NO;
    else
        return YES;
}

@end

That's it. You can start using these category methods in your ViewControllers just by importing import "NSString+Utilities.h"

Cheers

No comments:

Post a Comment