Skip to content
This repository has been archived by the owner on Jun 2, 2018. It is now read-only.

Commit

Permalink
Put NSParameterAssert all over the place to prevent crashes, exceptio…
Browse files Browse the repository at this point in the history
…ns instead; far more explicit.

Signed-off-by: Zachary Waldowski <zwaldowski@gmail.com>
  • Loading branch information
zwaldowski committed Dec 20, 2011
1 parent ad5620e commit eccc522
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 47 deletions.
14 changes: 14 additions & 0 deletions BlocksKit/NSArray+BlocksKit.m
Expand Up @@ -8,18 +8,24 @@
@implementation NSArray (BlocksKit)

- (void)each:(BKSenderBlock)block {
NSParameterAssert(block);

[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
block(obj);
}];
}

- (void)apply:(BKSenderBlock)block {
NSParameterAssert(block);

[self enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
block(obj);
}];
}

- (id)match:(BKValidationBlock)block {
NSParameterAssert(block);

NSUInteger index = [self indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return block(obj);
}];
Expand All @@ -31,6 +37,8 @@ - (id)match:(BKValidationBlock)block {
}

- (NSArray *)select:(BKValidationBlock)block {
NSParameterAssert(block);

NSArray *result = [self objectsAtIndexes:[self indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return block(obj);
}]];
Expand All @@ -42,6 +50,8 @@ - (NSArray *)select:(BKValidationBlock)block {
}

- (NSArray *)reject:(BKValidationBlock)block {
NSParameterAssert(block);

NSArray *result = [self objectsAtIndexes:[self indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return !block(obj);
}]];
Expand All @@ -53,6 +63,8 @@ - (NSArray *)reject:(BKValidationBlock)block {
}

- (NSArray *)map:(BKTransformBlock)block {
NSParameterAssert(block);

NSMutableArray *result = [NSMutableArray arrayWithCapacity:self.count];

[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
Expand All @@ -67,6 +79,8 @@ - (NSArray *)map:(BKTransformBlock)block {
}

- (id)reduce:(id)initial withBlock:(BKAccumulationBlock)block {
NSParameterAssert(block);

__block id result = nil;
BK_SET_RETAINED(result, initial);

Expand Down
10 changes: 10 additions & 0 deletions BlocksKit/NSDictionary+BlocksKit.m
Expand Up @@ -8,18 +8,24 @@
@implementation NSDictionary (BlocksKit)

- (void)each:(BKKeyValueBlock)block {
NSParameterAssert(block);

[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
block(key, obj);
}];
}

- (void)apply:(BKKeyValueBlock)block {
NSParameterAssert(block);

[self enumerateKeysAndObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id key, id obj, BOOL *stop) {
block(key, obj);
}];
}

- (NSDictionary *)select:(BKKeyValueValidationBlock)block {
NSParameterAssert(block);

NSMutableDictionary *list = [NSMutableDictionary dictionaryWithCapacity:self.count];

[self each:^(id key, id obj) {
Expand All @@ -34,6 +40,8 @@ - (NSDictionary *)select:(BKKeyValueValidationBlock)block {
}

- (NSDictionary *)reject:(BKKeyValueValidationBlock)block {
NSParameterAssert(block);

NSMutableDictionary *list = [NSMutableDictionary dictionaryWithCapacity:self.count];

[self each:^(id key, id obj) {
Expand All @@ -48,6 +56,8 @@ - (NSDictionary *)reject:(BKKeyValueValidationBlock)block {
}

- (NSDictionary *)map:(BKKeyValueTransformBlock)block {
NSParameterAssert(block);

NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity:self.count];

[self each:^(id key, id obj) {
Expand Down
14 changes: 13 additions & 1 deletion BlocksKit/NSIndexSet+BlocksKit.m
Expand Up @@ -8,24 +8,32 @@
@implementation NSIndexSet (BlocksKit)

- (void)each:(BKIndexBlock)block {
NSParameterAssert(block);

[self enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
block(idx);
}];
}

- (void)apply:(BKIndexBlock)block {
NSParameterAssert(block);

[self enumerateIndexesWithOptions:NSEnumerationConcurrent usingBlock:^(NSUInteger idx, BOOL *stop) {
block(idx);
}];
}

- (NSUInteger)match:(BKIndexValidationBlock)block {
NSParameterAssert(block);

return [self indexPassingTest:^BOOL(NSUInteger idx, BOOL *stop) {
return block(idx);
}];
}

- (NSIndexSet *)select:(BKIndexValidationBlock)block {
NSParameterAssert(block);

NSIndexSet *list = [self indexesPassingTest:^BOOL(NSUInteger idx, BOOL *stop) {
return block(idx);
}];
Expand All @@ -36,7 +44,9 @@ - (NSIndexSet *)select:(BKIndexValidationBlock)block {
return list;
}

- (NSIndexSet *)reject:(BKIndexValidationBlock)block {
- (NSIndexSet *)reject:(BKIndexValidationBlock)block {
NSParameterAssert(block);

NSIndexSet *list = [self indexesPassingTest:^BOOL(NSUInteger idx, BOOL *stop) {
return !block(idx);
}];
Expand All @@ -48,6 +58,8 @@ - (NSIndexSet *)reject:(BKIndexValidationBlock)block {
}

- (NSIndexSet *)map:(BKIndexTransformBlock)block {
NSParameterAssert(block);

NSMutableIndexSet *list = [NSMutableIndexSet indexSet];

[self enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
Expand Down
1 change: 1 addition & 0 deletions BlocksKit/NSInvocation+BlocksKit.m
Expand Up @@ -47,6 +47,7 @@ - (void)dealloc {
@implementation NSInvocation (BlocksKit)

+ (NSInvocation *)invocationWithTarget:(id)target block:(BKSenderBlock)block {
NSParameterAssert(block);
BKInvocationGrabber *grabber = [BKInvocationGrabber grabberWithTarget:target];
block(grabber);
return grabber.invocation;
Expand Down
6 changes: 6 additions & 0 deletions BlocksKit/NSMutableArray+BlocksKit.m
Expand Up @@ -8,6 +8,8 @@
@implementation NSMutableArray (BlocksKit)

- (void)performSelect:(BKValidationBlock)block {
NSParameterAssert(block);

NSIndexSet *list = [self indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return !block(obj);
}];
Expand All @@ -19,6 +21,8 @@ - (void)performSelect:(BKValidationBlock)block {
}

- (void)performReject:(BKValidationBlock)block {
NSParameterAssert(block);

NSIndexSet *list = [self indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return block(obj);
}];
Expand All @@ -30,6 +34,8 @@ - (void)performReject:(BKValidationBlock)block {
}

- (void)performMap:(BKTransformBlock)block {
NSParameterAssert(block);

NSMutableArray *new = BK_AUTORELEASE([self mutableCopy]);

[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
Expand Down
9 changes: 9 additions & 0 deletions BlocksKit/NSMutableDictionary+BlocksKit.m
Expand Up @@ -8,6 +8,9 @@
@implementation NSMutableDictionary (BlocksKit)

- (void)performSelect:(BKKeyValueValidationBlock)block {
NSParameterAssert(block);


NSMutableArray *keys = [NSMutableArray arrayWithCapacity:self.count];

[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
Expand All @@ -19,6 +22,9 @@ - (void)performSelect:(BKKeyValueValidationBlock)block {
}

- (void)performReject:(BKKeyValueValidationBlock)block {
NSParameterAssert(block);


NSMutableArray *keys = [NSMutableArray arrayWithCapacity:self.count];

[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
Expand All @@ -30,6 +36,9 @@ - (void)performReject:(BKKeyValueValidationBlock)block {
}

- (void)performMap:(BKKeyValueTransformBlock)block {
NSParameterAssert(block);


NSMutableDictionary *new = BK_AUTORELEASE([self mutableCopy]);

[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
Expand Down
9 changes: 9 additions & 0 deletions BlocksKit/NSMutableIndexSet+BlocksKit.m
Expand Up @@ -8,6 +8,9 @@
@implementation NSMutableIndexSet (BlocksKit)

- (void)performSelect:(BKIndexValidationBlock)block {
NSParameterAssert(block);


NSIndexSet *list = [self indexesPassingTest:^BOOL(NSUInteger idx, BOOL *stop) {
return !block(idx);
}];
Expand All @@ -19,6 +22,9 @@ - (void)performSelect:(BKIndexValidationBlock)block {
}

- (void)performReject:(BKIndexValidationBlock)block {
NSParameterAssert(block);


NSIndexSet *list = [self indexesPassingTest:^BOOL(NSUInteger idx, BOOL *stop) {
return block(idx);
}];
Expand All @@ -30,6 +36,9 @@ - (void)performReject:(BKIndexValidationBlock)block {
}

- (void)performMap:(BKIndexTransformBlock)block {
NSParameterAssert(block);


NSMutableIndexSet *new = BK_AUTORELEASE([self mutableCopy]);

[self enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
Expand Down
8 changes: 8 additions & 0 deletions BlocksKit/NSMutableSet+BlocksKit.m
Expand Up @@ -8,6 +8,8 @@
@implementation NSMutableSet (BlocksKit)

- (void)performSelect:(BKValidationBlock)block {
NSParameterAssert(block);

NSSet *list = [self objectsPassingTest:^BOOL(id obj, BOOL *stop) {
return block(obj);
}];
Expand All @@ -16,6 +18,9 @@ - (void)performSelect:(BKValidationBlock)block {
}

- (void)performReject:(BKValidationBlock)block {
NSParameterAssert(block);


NSSet *list = [self objectsPassingTest:^BOOL(id obj, BOOL *stop) {
return !block(obj);
}];
Expand All @@ -24,6 +29,9 @@ - (void)performReject:(BKValidationBlock)block {
}

- (void)performMap:(BKTransformBlock)block {
NSParameterAssert(block);


NSMutableSet *new = [NSMutableSet setWithCapacity:self.count];

[self enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
Expand Down
11 changes: 11 additions & 0 deletions BlocksKit/NSObject+BlockObservation.m
Expand Up @@ -67,6 +67,10 @@ - (NSString *)addObserverForKeyPath:(NSString *)keyPath task:(BKObservationBlock
}

- (void)addObserverForKeyPath:(NSString *)keyPath identifier:(NSString *)identifier task:(BKObservationBlock)task {
NSParameterAssert(keyPath);
NSParameterAssert(identifier);
NSParameterAssert(task);

__block BKObserver *newObserver = nil;

dispatch_sync(BKObserverMutationQueue(), ^{
Expand All @@ -91,6 +95,10 @@ - (NSString *)addObserverForKeyPath:(NSString *)keyPath options:(NSKeyValueObser
}

- (void)addObserverForKeyPath:(NSString *)keyPath identifier:(NSString *)identifier options:(NSKeyValueObservingOptions)options task:(BKObservationBlock)task {
NSParameterAssert(keyPath);
NSParameterAssert(identifier);
NSParameterAssert(task);

__block BKObserver *newObserver = nil;

dispatch_sync(BKObserverMutationQueue(), ^{
Expand All @@ -109,6 +117,9 @@ - (void)addObserverForKeyPath:(NSString *)keyPath identifier:(NSString *)identif
}

- (void)removeObserverForKeyPath:(NSString *)keyPath identifier:(NSString *)identifier {
NSParameterAssert(keyPath);
NSParameterAssert(identifier);

dispatch_sync(BKObserverMutationQueue(), ^{
NSString *token = [NSString stringWithFormat:@"%@_%@", keyPath, identifier];
NSMutableDictionary *dict = [self associatedValueForKey:&kObserverBlocksKey];
Expand Down
24 changes: 10 additions & 14 deletions BlocksKit/NSObject+BlocksKit.m
Expand Up @@ -6,21 +6,18 @@
#import "NSObject+BlocksKit.h"
#import <objc/runtime.h>

typedef void(^BKInternalWrappingBlock)(BOOL cancel);
typedef void(^BKInternalWrappingBlock)(BOOL);

static inline dispatch_time_t BKTimeDelay(NSTimeInterval time) {
int64_t delta = (NSEC_PER_SEC * time);
return dispatch_time(DISPATCH_TIME_NOW, delta);
}
#define BKTimeDelay(t) dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * t)

@implementation NSObject (BlocksKit)

- (id)performBlock:(BKSenderBlock)block afterDelay:(NSTimeInterval)delay {
if (!block) return nil;
NSParameterAssert(block);

__block BOOL cancelled = NO;

BKInternalWrappingBlock wrapper = ^(BOOL cancel) {
void(^wrapper)(BOOL) = ^(BOOL cancel) {
if (cancel) {
cancelled = YES;
return;
Expand All @@ -36,11 +33,11 @@ - (id)performBlock:(BKSenderBlock)block afterDelay:(NSTimeInterval)delay {
}

+ (id)performBlock:(BKBlock)block afterDelay:(NSTimeInterval)delay {
if (!block) return nil;
NSParameterAssert(block);

__block BOOL cancelled = NO;

BKInternalWrappingBlock wrapper = ^(BOOL cancel) {
void(^wrapper)(BOOL) = ^(BOOL cancel) {
if (cancel) {
cancelled = YES;
return;
Expand All @@ -54,10 +51,9 @@ + (id)performBlock:(BKBlock)block afterDelay:(NSTimeInterval)delay {
}

+ (void)cancelBlock:(id)block {
if (!block)
return;
BKInternalWrappingBlock wrapper = block;
wrapper(YES);
NSParameterAssert(block);
void(^wrapper)(BOOL) = block;
wrapper(YES);
}

@end

0 comments on commit eccc522

Please sign in to comment.