-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNSObject+MMBindings.m
70 lines (56 loc) · 1.93 KB
/
NSObject+MMBindings.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
//
// NSObject+MMBindings.m
// SteamEngine
//
// Created by Rob Keniger on 31/07/09.
// Copyright 2009 Big Bang Software Pty Ltd. All rights reserved.
//
#import "NSObject+MMBindings.h"
@implementation NSObject (MMBindings)
- (void)propagateValue:(id)value forBinding:(NSString*)binding;
{
NSParameterAssert(binding != nil);
//WARNING: bindingInfo contains NSNull, so it must be accounted for
NSDictionary* bindingInfo = [self infoForBinding:binding];
if(!bindingInfo)
return; //there is no binding
//apply the value transformer, if one has been set
NSDictionary* bindingOptions = [bindingInfo objectForKey:NSOptionsKey];
if(bindingOptions)
{
NSValueTransformer* transformer = [bindingOptions valueForKey:NSValueTransformerBindingOption];
if(!transformer || (id)transformer == [NSNull null])
{
NSString* transformerName = [bindingOptions valueForKey:NSValueTransformerNameBindingOption];
if(transformerName && (id)transformerName != [NSNull null])
{
transformer = [NSValueTransformer valueTransformerForName:transformerName];
}
}
if(transformer && (id)transformer != [NSNull null])
{
if([[transformer class] allowsReverseTransformation])
{
value = [transformer reverseTransformedValue:value];
}
else
{
NSLog(@"WARNING: binding \"%@\" has value transformer, but it doesn't allow reverse transformations in %s", binding, __PRETTY_FUNCTION__);
}
}
}
id boundObject = [bindingInfo objectForKey:NSObservedObjectKey];
if(!boundObject || boundObject == [NSNull null])
{
NSLog(@"ERROR: NSObservedObjectKey was nil for binding \"%@\" in %s", binding, __PRETTY_FUNCTION__);
return;
}
NSString* boundKeyPath = [bindingInfo objectForKey:NSObservedKeyPathKey];
if(!boundKeyPath || (id)boundKeyPath == [NSNull null])
{
NSLog(@"ERROR: NSObservedKeyPathKey was nil for binding \"%@\" in %s", binding, __PRETTY_FUNCTION__);
return;
}
[boundObject setValue:value forKeyPath:boundKeyPath];
}
@end