Skip to content

Instantly share code, notes, and snippets.

@ishaq
Last active December 28, 2015 20:49
Show Gist options
  • Save ishaq/7560227 to your computer and use it in GitHub Desktop.
Save ishaq/7560227 to your computer and use it in GitHub Desktop.
useful NSString functions
#import <Foundation/Foundation.h>
/**
* this category contains functions that either work on `NSString` or return `NSString`. The category is called <b>Kahaf</b>
* because these functions don't exactly fit into any other single category.
*/
@interface NSString (Kahaf)
/**
* Creates and returns a new UUID with RFC 4122 version 4 random bytes.
*
* @return a string representation of the generated UUID
*/
+ (NSString *)uniqueID;
/**
* returns path to the `Documents` directory
*
* @return `Documents` directory path
*/
+ (NSString *)pathForPublicDocumentDirectory;
/**
* returns path to the `Library` directory
*
* @return `Library` directory path
*/
+ (NSString *)pathForPrivateDocumentDirectory;
/**
* returns path to the `Cache` directory
*
* @return `Cache` directory path
*/
+ (NSString *)pathForCacheDirectory;
/**
* returns path to a temporary directory, it is safe to write to this directory without checking for existing files
*
* @return path to a temporary directory
*/
+ (NSString *)pathForTemporaryDirectory;
/**
* prepends path to `Documents` directory to `self` and returns a new `NSString`
*
* @return `NSString` with `Documents` directory prepended
*/
- (NSString *)prependPublicDocumentsDirectory;
/**
* prepends path to `Library` directory to `self` and returns a new `NSString`
*
* @return `NSString` with `Library` directory prepended
*/
- (NSString *)prependPrivateDocumentsDirectory;
/**
* prepends path to a temporary directory to `self` and returns a new `NSString`
*
* @return `NSString` with path to a temporary directory prepended
*/
- (NSString *)prependTemporaryDirectory;
/**
* removes white space from the start and end of the string (left and right trim)
*
* @return a new string with white space from both ends removed
*/
- (NSString *)trimWhitespace;
/**
* checks whether string is a valid email
*
* @return returns YES if string is a valid email
*/
- (BOOL)isValidEmail;
- (BOOL)isValidURL;
/**
* removes white space from both ends of a string and returns length
*
* @return returns length of the string (after removing whitespace from both ends)
*/
- (NSUInteger)trimmedLength;
- (NSNumber *)numberWithDecimalString;
@end
#import "NSString+Kahaf.h"
@implementation NSString (Kahaf)
+ (NSString *)uniqueID
{
return [[NSUUID UUID] UUIDString];
}
+ (NSString *)pathForPublicDocumentDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}
+ (NSString*)pathForPrivateDocumentDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
return libraryDirectory;
}
+ (NSString *)pathForCacheDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES);
NSString *cacheDir = [paths objectAtIndex:0];
return cacheDir;
}
+ (NSString *)pathForTemporaryDirectory
{
return NSTemporaryDirectory();
}
- (NSString *)prependPublicDocumentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:self];
return appFile;
}
- (NSString *)prependPrivateDocumentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:self];
return appFile;
}
- (NSString *)prependTemporaryDirectory
{
NSString *path = NSTemporaryDirectory();
return [path stringByAppendingPathComponent:self];
}
- (NSString *)trimWhitespace
{
return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
// based on: http://is.gd/rMQNFX
- (BOOL)isValidEmail
{
NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stricterFilterString];
return [emailTest evaluateWithObject:self];
}
// based on: https://stackoverflow.com/questions/1471201/how-to-validate-an-url-on-the-iphone
- (BOOL)isValidURL
{
if([self trimmedLength] > 0)
{
NSURL *website = [NSURL URLWithString:self];
if(website && website.scheme && website.host)
{
return YES;
}
}
return NO;
}
- (NSUInteger)trimmedLength
{
return [[self trimWhitespace] length];
}
- (NSNumber *)numberWithDecimalString
{
static NSNumberFormatter *f = nil;
if(f == nil)
{
f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
}
return [f numberFromString:self];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment