测试已测试 | ✓ |
Lang语言 | Obj-CObjective C |
许可证 | MIT |
发布最新发布 | 2015年1月 |
由 Seivan Heidari 维护。
此库是作为
SHFoundationAdditions
的组成部分使用的,用于改进 iOS 应用架构的多个组件,包括 Foundation、UIKit、CoreLocation、GameKit、MapKit 以及其他方面。
此外,还有 for UIKit
,用于通过 block 选项扩展 UIKit 的大多数组件。
基于 NSObject 之上的数据绑定和键值观察,使用 block。block 以弱引用形式持有,因此您不需要在对象消失时进行清理。有一个自动的 Swizzling 配置,您可以在每个类的基础上切换。
pod 'SHKeyValueObserverBlocks'
将此置于特定文件或项目前缀文件中
#import "NSObject+SHKeyValueObserverBlocks.h"
或者
#import "SHKeyValueObserverBlocks.h"
NSString * path = @"languagesArray";
NSString * identifier = [self SH_addObserverForKeyPath:path
block:^(
NSKeyValueChange changeType,
id oldValue,
id newValue,
NSIndexPath *indexPath) {
switch (changeType) {
case NSKeyValueChangeSetting:
NSLog(@"Setting %@", newValue);
break;
case NSKeyValueChangeInsertion:
NSLog(@"Inserting %@", newValue);
break;
case NSKeyValueChangeRemoval:
NSLog(@"Removal %@", oldValue);
break;
case NSKeyValueChangeReplacement:
NSLog(@"ChangeReplacement %@ with %@", oldValue, newValue);
break;
default:
break;
}
}];
NSLog(@"Starting with NSArray");
self.languagesArray = @[@"Python"];
[[self mutableArrayValueForKey:path] addObject:@"C++"];
[[self mutableArrayValueForKey:path] addObject:@"Objective-c"];
[[self mutableArrayValueForKey:path] replaceObjectAtIndex:0 withObject:@"Ruby"];
[[self mutableArrayValueForKey:path] removeObject:@"C++"];
NSLog(@"%@", self.languagesArray);
NSString * identifier = [self SH_setBindingUniObserverKeyPath:@"playersDictionary"
toObject:self
withKeyPath:@"othersDictionary"
transformValueBlock:^id(
id object,
NSString *keyPath,
id newValue,
BOOL *shouldAbort) {
return ((NSObject *)newValue).mutableCopy ;
}];
NSArray * identifiers = [self.model SH_setBindingObserverKeyPath:@"errors"
toObject:self
withKeyPath:@"errors"];
NSArray * identifiers = [self.model SH_setBindingObserverKeyPath:@"errors"
toObject:self
withKeyPath:SHKey(errors)"];
#pragma mark - Block Definitions
typedef void (^SHKeyValueObserverSplatBlock)(NSKeyValueChange changeType,
id oldValue, id newValue,
NSIndexPath * indexPath);
typedef void (^SHKeyValueObserverDefaultBlock)(NSString * keyPath,
NSDictionary * change);
typedef id(^SHKeyValueObserverBindingTransformBlock)(id object,
NSString * keyPath,
id newValue,
BOOL *shouldAbort);
@interface NSObject (SHKeyValueObserverBlocks)
#pragma mark - Config
+(BOOL)SH_isAutoRemovingObservers;
+(void)SH_setAutoRemovingObservers:(BOOL)isAutoRemovingObservers;
#pragma mark - Properties
@property(nonatomic,readonly) NSDictionary * SH_observedKeyPaths;
#pragma mark - Add Observers
-(NSString *)SH_addObserverForKeyPath:(NSString *)theKeyPath
block:(SHKeyValueObserverSplatBlock)theBlock;
-(NSString *)SH_addObserverForKeyPaths:(NSArray *)theKeyPaths
withOptions:(NSKeyValueObservingOptions)theOptions
block:(SHKeyValueObserverDefaultBlock)theBlock;
#pragma mark - Set Bindings
-(NSArray *)SH_setBindingObserverKeyPath:(NSString *)theKeyPath
toObject:(NSObject *)theObject
withKeyPath:(NSString *)theOtherKeyPath;
-(NSString *)SH_setBindingUniObserverKeyPath:(NSString *)theKeyPath
toObject:(NSObject *)theObject
withKeyPath:(NSString *)theOtherKeyPath;
-(NSString *)SH_setBindingUniObserverKeyPath:(NSString *)theKeyPath
toObject:(NSObject *)theObject
withKeyPath:(NSString *)theOtherKeyPath
transformValueBlock:(SHKeyValueObserverBindingTransformBlock)theBlock;
#pragma mark - Remove Observers
-(void)SH_removeAllObserversWithIdentifiers:(NSArray *)theIdentifiers;
-(void)SH_removeAllObserversForKeyPaths:(NSArray *)theKeyPaths;
-(void)SH_removeAllObservers;
@end
如果您在一个项目中使用了 SHKeyValueObserverBlocks,我将非常乐意听到消息。
email: [email protected]
twitter: @seivanheidari
SHKeyValueObserverBlocks 是 © 2013 Seivan 的财产,并且可以在 MIT 许可证 下自由分发。请参阅LICENSE.md
文件。