Skip to content

Instantly share code, notes, and snippets.

@vgrichina
Last active December 11, 2015 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgrichina/4657230 to your computer and use it in GitHub Desktop.
Save vgrichina/4657230 to your computer and use it in GitHub Desktop.
//
// CXBinder.h
// TheList
//
// Created by Vladimir Grichina on 28.01.13.
// Copyright (c) 2013 TheList, LLC. All rights reserved.
//
#import <THObserversAndBinders/THBinder.h>
@interface CXBinder : THBinder
@end
#define BIND(fromObj, fromPath, toObj, toPath) [CXBinder binderFromObject:fromObj keyPath:fromPath toObject:toObj keyPath:toPath]
#define BIND_T(fromObj, fromPath, toObj, toPath, transform) [CXBinder binderFromObject:fromObj keyPath:fromPath toObject:toObj keyPath:toPath transformationBlock:^id(id value) { return transform; }]
//
// CXBinder.m
// TheList
//
// Created by Vladimir Grichina on 28.01.13.
// Copyright (c) 2013 TheList, LLC. All rights reserved.
//
#import "CXBinder.h"
@interface THBinder(private)
- (id)initForBindingFromObject:(id)fromObject keyPath:(NSString *)fromKeyPath
toObject:(id)toObject keyPath:(NSString *)toKeyPath
transformationBlock:(THBinderTransformationBlock)transformationBlock;
@end
@implementation CXBinder
- (id)initForBindingFromObject:(id)fromObject keyPath:(NSString *)fromKeyPath
toObject:(id)toObject keyPath:(NSString *)toKeyPath
transformationBlock:(THBinderTransformationBlock)transformationBlock
{
if (self = [super initForBindingFromObject:fromObject keyPath:fromKeyPath
toObject:toObject keyPath:toKeyPath
transformationBlock:transformationBlock]) {
if (transformationBlock) {
[toObject setValue:transformationBlock([fromObject valueForKeyPath:fromKeyPath])
forKeyPath:toKeyPath];
} else {
[toObject setValue:[fromObject valueForKeyPath:fromKeyPath]
forKeyPath:toKeyPath];
}
}
return self;
}
- (void)dealloc
{
[self stopBinding];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment