-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSString+Utilities.m
More file actions
89 lines (73 loc) · 2.66 KB
/
NSString+Utilities.m
File metadata and controls
89 lines (73 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// NSString+Utilities.m
// xolaware utilities
//
// Created by me on 2012.04.09.
#include "xolawareOpenSourceCopyright.h" // Copyright (c) 2012 xolaware.
#import "NSString+Utilities.h"
@implementation NSString (Utilities)
- (BOOL)isNonEmpty {
return ![self isEqualToString:@""];
}
- (NSString*)stringByLocalizingThenAppending:(NSString*)stringToAppend {
return [NSLocalizedString(self, nil) stringByAppendingString:stringToAppend];
}
- (NSURL*)urlForMainBundleResourceHTML {
NSString* resPath = [[NSBundle mainBundle] pathForResource:self ofType:@"html"];
if (resPath)
return [NSURL fileURLWithPath:resPath];
return nil;
}
- (NSDictionary*)dictionaryInterpretingContentsAsHttpQuery {
NSArray* parameters = [self componentsSeparatedByString:@"&"];
if (2 > parameters.count || 0 != parameters.count % 2)
return nil;
NSMutableDictionary* items = [NSMutableDictionary dictionaryWithCapacity:parameters.count];
for (NSString* parameter in parameters)
{
NSArray* parameterKeyEqualValue = [parameter componentsSeparatedByString:@"="];
if (2 != parameterKeyEqualValue.count)
continue; // skip this one w.r.t inserting it in the dictionary
[items setValue:parameterKeyEqualValue[1] forKey:parameterKeyEqualValue[0]];
}
return items.count ? items.copy : nil;
}
- (BOOL)hasCharacterInSet:(NSCharacterSet*)charSet {
return NSNotFound != [self rangeOfCharacterFromSet:charSet].location;
}
- (BOOL)hasEmailTraits {
NSUInteger atSign = [self rangeOfString:@"@"].location;
NSUInteger dot = [self rangeOfString:@"." options:NSBackwardsSearch].location;
// cursory check, this should catch a bunch
return NSNotFound != atSign && NSNotFound != dot
&& atSign != 0 && atSign < (dot-1) && dot < self.length-2;
}
- (BOOL)hasNewline {
return [self hasCharacterInSet:[NSCharacterSet newlineCharacterSet]];
}
- (BOOL)hasWhitespace {
return [self hasCharacterInSet:[NSCharacterSet whitespaceCharacterSet]];
}
- (BOOL)hasWhitespaceOrNewline {
return [self hasCharacterInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (NSString*)uuidStringByCompactingExistingUUID {
return [[self stringByReplacingOccurrencesOfString:@"-" withString:@""] lowercaseString];
}
+ (NSString*)generateCompactUUID {
NSString* uuidStr;
if (UIDevice.currentDevice.systemVersion.floatValue >= 6.0)
uuidStr = [[NSUUID UUID] UUIDString];
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
else // if the above check kicks in, then we're no longer even building for pre-iOS6
{
CFUUIDRef u = CFUUIDCreate(NULL);
CFStringRef s = CFUUIDCreateString(NULL, u);
CFRelease(u);
uuidStr = ((__bridge NSString *)s).copy;
CFRelease(s);
}
#endif
return [uuidStr uuidStringByCompactingExistingUUID];
}
@end