This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
NSObject+HTPropertyHash.m
73 lines (64 loc) · 2.43 KB
/
NSObject+HTPropertyHash.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
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
//
// NSObject+HTPropertyHash.m
// HotelTonight
//
// Created by Jacob Jennings on 11/28/12.
// Copyright (c) 2012 Hotel Tonight. All rights reserved.
//
#import "NSObject+HTPropertyHash.h"
@implementation NSObject (HTPropertyHash)
- (NSString *)hashStringForKeyPaths:(NSArray *)keyPaths
{
NSMutableString *hashString = [[NSMutableString alloc] initWithString:NSStringFromClass([self class])];
for (NSString *keyPath in keyPaths)
{
NSObject *objectForKeyPath = [self valueForKeyPath:keyPath];
NSString *description = nil;
if ([objectForKeyPath isKindOfClass:[NSArray class]])
{
NSArray *array = ((NSArray *)objectForKeyPath);
NSMutableString *descriptionMutable = [[NSMutableString alloc] init];
[descriptionMutable appendString:@"( "];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[descriptionMutable appendString:[self descriptionForObject:obj keyPath:nil]];
if (idx < [array count] - 1)
{
[descriptionMutable appendString:@", "];
}
}];
[descriptionMutable appendString:@" )"];
description = [descriptionMutable copy];
}
else
{
description = [self descriptionForObject:objectForKeyPath keyPath:keyPath];
}
[hashString appendFormat:@"\n%@: %@", keyPath, description];
}
return hashString;
}
- (NSString *)descriptionForObject:(NSObject *)object keyPath:(NSString *)keyPath
{
if (keyPath
&& [self respondsToSelector:@selector(hashDescriptionForKeyPath:)])
{
return [(id<HTCustomHash>)self hashDescriptionForKeyPath:keyPath];
}
else if ([object respondsToSelector:@selector(hashDescription)])
{
return [(id<HTCustomHash>)object hashDescription];
} else if (object && CFGetTypeID((__bridge CFTypeRef)object) == CGColorGetTypeID()) {
CGColorRef colorRef = (CGColorRef)CFBridgingRetain(object);
return [self descriptionForCGColor:colorRef];
}
else
{
return [object description];
}
}
- (NSString *)descriptionForCGColor:(CGColorRef)colorRef;
{
const CGFloat *colorComponents = CGColorGetComponents(colorRef);
return [NSString stringWithFormat:@"(%f %f %f %f)", colorComponents[0], colorComponents[1], colorComponents[2], colorComponents[3]];
}
@end