CPDictionary enumerateKeysAndObjectsUsingBlock:.

This commit is contained in:
Alexander Ljungberg committed 2012-07-17 20:48:52 +01:00
1 parent fea0299033
commit 5724dfe21d
2 files changed
+62

No files matched your search

+26
View File
@@ -654,6 +654,32 @@
var value = [self objectForKey:aKey];
return ((value !== nil) && (value !== undefined));
}
- (void)enumerateKeysAndObjectsUsingBlock:(Function /*(id aKey, id anObject, @ref BOOL stop)*/)aFunction
{
var shouldStop = NO,
shouldStopRef = AT_REF(shouldStop);
for (var index = 0; index < _count; index++)
{
var key = _keys[index],
value = valueForKey(key);
aFunction(key, value, shouldStopRef);
if (shouldStop)
return;
}
}
- (void)enumerateKeysAndObjectsWithOptions:(CPEnumerationOptions)opts usingBlock:(Function /*(id aKey, id anObject, @ref BOOL stop)*/)aFunction
{
// Ignore the options because neither option has an effect.
// CPEnumerationReverse has no effect on enumerating a CPDictionary because dictionary enumeration is not ordered.
// CPEnumerationConcurrent is not possible in a single threaded environment.
[self enumerateKeysAndObjectsUsingBlock:aFunction];
}
@end
@implementation CPDictionary (CPCoding)
+36
View File
@@ -348,6 +348,42 @@
[self assert:expected equals:result];
}
- (void)testEnumerateKeysAndObjectsUsingBlock_
{
var input0 = [CPDictionary dictionary],
input1 = [CPDictionary dictionaryWithJSObject:{a: 1, b: 3, c: "b"}],
output = [CPMutableDictionary dictionary],
outputFunction = function(aKey, anObject)
{
[output setValue:anObject forKey:aKey];
};
[input0 enumerateKeysAndObjectsUsingBlock:outputFunction];
[self assert:0 equals:[output count] message:@"output when enumerating empty dictionary"];
[input1 enumerateKeysAndObjectsUsingBlock:outputFunction];
[self assert:3 equals:[output count] message:@"output when enumerating input1"];
[self assert:input1 equals:output message:@"output should equal input"];
// Stop enumerating after two keys.
output = [CPMutableDictionary dictionary];
stoppingFunction = function(aKey, anObject, stop)
{
[output setValue:anObject forKey:aKey];
if ([output count] > 1)
stop(YES); // AT_DEREF(stop, YES) - FIXME Replace with proper @ref @deref when in ObjJ.
}
[input1 enumerateKeysAndObjectsUsingBlock:stoppingFunction];
[self assert:2 equals:[output count] message:@"output when enumerating input1 and stopping after 2"];
// CPEnumerationReverse shouldn't have any particular effect. Just check that it doesn't crash.
output = [CPMutableDictionary dictionary];
[input1 enumerateKeysAndObjectsWithOptions:CPEnumerationReverse usingBlock:outputFunction];
[self assert:3 equals:[output count] message:@"output when enumerating input1"];
[self assert:input1 equals:output message:@"output should equal input"];
}
- (void)testJSObjectDescription
{
var dict = [[CPDictionary alloc] initWithObjects:[CGRectMake(1, 2, 3, 4), CGPointMake(5, 6)] forKeys:[@"key1", @"key2"]],