Fix for adding initWithObjectsAndKeys: and dictionaryWithObjectsAndKeys: to CPDictionary.

Made a few minor changes for speed to Vladimir's commit.

Closes #38.

Reviewed by me.
This commit is contained in:
Vladimir Pouzanov authored and Francisco Tolmasky committed 2009-05-03 16:52:56 -07:00
1 parent ad866e44f5
commit 2176785370
1 file changed
+67
+67
View File
@@ -155,6 +155,31 @@
return dictionary;
}
/*!
Creates and returns a dictionary constructed by a given pairs of keys and values.
@param firstObject first object value
@param ... key for the first object and ongoing value-key pairs for more objects.
@throws CPInvalidArgumentException if the number of objects and keys is different
@return the new CPDictionary
Assuming that there's no object retaining in Cappuccino, you can create
dictionaries same way as with alloc and initWithObjectsAndKeys:
var dict = [CPDictionary dictionaryWithObjectsAndKeys:
@"value1", @"key1",
@"value2", @"key2"];
Note, that there's no final nil like in Objective-C/Cocoa.
@see [CPDictionary initWithObjectsAndKeys:]
*/
+ (id)dictionaryWithObjectsAndKeys:(id)firstObject, ...
{
arguments[0] = [self alloc];
arguments[1] = @selector(initWithObjectsAndKeys:);
return objj_msgSend.apply(this, arguments);
}
/*!
Initializes the dictionary with the contents of another dictionary.
@param aDictionary the dictionary to copy key-value pairs from
@@ -196,6 +221,48 @@
return self;
}
/*!
Creates and returns a dictionary constructed by a given pairs of keys and values.
@param firstObject first object value
@param ... key for the first object and ongoing value-key pairs for more objects.
@throws CPInvalidArgumentException if the number of objects and keys is different
@return the new CPDictionary
You can create dictionaries this way:
var dict = [[CPDictionary alloc] initWithObjectsAndKeys:
@"value1", @"key1",
@"value2", @"key2"];
Note, that there's no final nil like in Objective-C/Cocoa.
*/
- (id)initWithObjectsAndKeys:(id)firstObject, ...
{
var argCount = arguments.length;
if (argCount % 2 !== 0)
[CPException raise:CPInvalidArgumentException reason:"Key-value count is mismatched. (" + argCount + " arguments passed)"];
self = [super init];
if (self)
{
// The arguments array contains self and _cmd, so the first object is at position 2.
var index = 2;
for(; index < argCount; index += 2)
{
var value = arguments[index];
if (value === nil)
break;
[self setObject:value forKey:arguments[index + 1]];
}
}
return self;
}
/*!
return a copy of the receiver (does not deep copy the objects contained in the dictionary).
*/