-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSObject+BindingPropagation.m
59 lines (48 loc) · 1.95 KB
/
NSObject+BindingPropagation.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
//
// NSObject+BindingPropagation.m
// Truth Tables
//
// Created by Samuel Williams on 20/11/11.
// Copyright (c) 2011 Orion Transfer Ltd. All rights reserved.
//
#import "NSObject+BindingPropagation.h"
#import <AppKit/NSKeyValueBinding.h>
@implementation NSObject (BindingPropagation)
-(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[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[NSObservedObjectKey];
if (!boundObject || boundObject == [NSNull null]) {
NSLog(@"ERROR: NSObservedObjectKey was nil for binding \"%@\" in %s", binding, __PRETTY_FUNCTION__);
return;
}
NSString* boundKeyPath = bindingInfo[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