Last active
August 29, 2015 13:56
-
-
Save r3econ/9135312 to your computer and use it in GitHub Desktop.
NSString Extensions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSString (RAFExtensions) | |
/** | |
Returns random alphanumeric string with a given length. | |
*/ | |
+ (NSString *)randomStringWithLength:(NSUInteger)length; | |
/** | |
Returns YES if the string is a valid email address of form: example@example.com | |
*/ | |
- (BOOL)isValidEmailString; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSString+RAFExtensions.h" | |
@implementation NSString (RAFExtensions) | |
#pragma mark - Random string | |
+ (NSString *)randomStringWithLength:(NSUInteger)length | |
{ | |
NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | |
NSMutableString *randomString = [NSMutableString stringWithCapacity:length]; | |
for (int i = 0; i < length; i++) | |
{ | |
[randomString appendFormat:@"%C", [letters characterAtIndex:arc4random() % [letters length]]]; | |
} | |
return randomString; | |
} | |
#pragma mark - Email validation | |
- (BOOL)isValidEmailString | |
{ | |
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; | |
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; | |
return [emailTest evaluateWithObject:self]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment