-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathNSIndexPathX.m
38 lines (27 loc) · 845 Bytes
/
NSIndexPathX.m
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
#import "NSIndexPathX.h"
@implementation NSIndexPath (X)
+ (NSIndexPath *)indexPathWithString:(NSString *)path
{
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:NULL length:0];
for (NSString *component in [path componentsSeparatedByString:@"."])
indexPath = [indexPath indexPathByAddingIndex:component.integerValue];
return indexPath;
}
- (NSString *)stringValue
{
if (!self.length)
return @"";
NSMutableString *path = [NSMutableString.alloc initWithFormat:@"%lu", (unsigned long)self.firstIndex];
for (int i = 1; i < self.length; ++i)
[path appendFormat:@".%lu", (unsigned long)[self indexAtPosition:i]];
return path;
}
- (NSUInteger)firstIndex
{
return [self indexAtPosition:0];
}
- (NSUInteger)lastIndex
{
return [self indexAtPosition:self.length - 1];
}
@end