Index: src/OFDictionary.h ================================================================== --- src/OFDictionary.h +++ src/OFDictionary.h @@ -196,10 +196,24 @@ * @return The object for the given key or `nil` if the key was not found */ - (nullable ObjectType)objectForKey: (KeyType)key; - (nullable ObjectType)objectForKeyedSubscript: (KeyType)key; +/*! + * @brief Returns the value for the given key or `nil` if the key was not + * found. + * + * If the key starts with an `@`, the `@` is stripped and + * `[super valueForKey:]` is called. + * If the key does not start with an `@`, this is equivalent to + * @ref objectForKey:. + * + * @param key The key whose value should be returned + * @return The value for the given key or `nil` if the key was not found + */ +- (nullable id)valueForKey: (OFString*)key; + /*! * @brief Checks whether the dictionary contains an object equal to the * specified object. * * @param object The object which is checked for being in the dictionary Index: src/OFDictionary.m ================================================================== --- src/OFDictionary.m +++ src/OFDictionary.m @@ -277,10 +277,27 @@ OF_UNRECOGNIZED_SELECTOR } - (id)objectForKeyedSubscript: (id)key { + return [self objectForKey: key]; +} + +- (id)valueForKey: (OFString*)key +{ + if ([key hasPrefix: @"@"]) { + void *pool = objc_autoreleasePoolPush(); + id ret; + + key = [key substringWithRange: of_range(1, [key length] - 1)]; + ret = [[super valueForKey: key] retain]; + + objc_autoreleasePoolPop(pool); + + return [ret autorelease]; + } + return [self objectForKey: key]; } - (size_t)count { Index: src/OFKeyValueCoding.h ================================================================== --- src/OFKeyValueCoding.h +++ src/OFKeyValueCoding.h @@ -12,11 +12,15 @@ * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ +#import "macros.h" + @class OFString; + +OF_ASSUME_NONNULL_BEGIN /*! * @protocol OFKeyValueCoding OFKeyValueCoding.h ObjFW/OFKeyValueCoding.h * * @brief A protocol for Key Value Coding. @@ -29,11 +33,11 @@ * @brief Return the value for the specified key * * @param key The key of the value to return * @return The value for the specified key */ -- (id)valueForKey: (OFString*)key; +- (nullable id)valueForKey: (OFString*)key; /*! * @brief This is called by @ref valueForKey: if the specified key does not * exist. * @@ -40,19 +44,19 @@ * By default, this throws an @ref OFUndefinedKeyException. * * @param key The undefined key of the value to return * @return The value for the specified undefined key */ -- (id)valueForUndefinedKey: (OFString*)key; +- (nullable id)valueForUndefinedKey: (OFString*)key; /*! * @brief Set the value for the specified key * * @param value The value for the specified key * @param key The key of the value to set */ -- (void)setValue: (id)value +- (void)setValue: (nullable id)value forKey: (OFString*)key; /*! * @brief This is called by @ref setValue:forKey: if the specified key does not * exist. @@ -60,8 +64,10 @@ * By default, this throws an @ref OFUndefinedKeyException. * * @param value The value for the specified undefined key * @param key The undefined key of the value to set */ -- (void)setValue: (id)value +- (void)setValue: (nullable id)value forUndefinedKey: (OFString*)key; @end + +OF_ASSUME_NONNULL_END Index: src/OFMutableDictionary.h ================================================================== --- src/OFMutableDictionary.h +++ src/OFMutableDictionary.h @@ -75,10 +75,24 @@ - (void)setObject: (ObjectType)object forKey: (KeyType)key; - (void)setObject: (ObjectType)object forKeyedSubscript: (KeyType)key; +/*! + * @brief Sets a value for a key. + * + * If the key starts with an `@`, the `@` is stripped and + * `[super setValue:forKey:]` is called. + * If the key does not start with an `@`, this is equivalent to + * @ref setObject:forKey:. + * + * @param key The key to set + * @param value The value to set the key to + */ +- (void)setValue: (nullable id)value + forKey: (OFString*)key; + /*! * @brief Removes the object for the specified key from the dictionary. * * @param key The key whose object should be removed */ Index: src/OFMutableDictionary.m ================================================================== --- src/OFMutableDictionary.m +++ src/OFMutableDictionary.m @@ -18,10 +18,11 @@ #include #import "OFMutableDictionary_hashtable.h" #import "OFArray.h" +#import "OFString.h" static struct { Class isa; } placeholder; @@ -173,10 +174,28 @@ forKeyedSubscript: (id)key { [self setObject: object forKey: key]; } + +- (void)setValue: (id)value + forKey: (OFString*)key +{ + if ([key hasPrefix: @"@"]) { + void *pool = objc_autoreleasePoolPush(); + + key = [key substringWithRange: of_range(1, [key length] - 1)]; + [super setValue: value + forKey: key]; + + objc_autoreleasePoolPop(pool); + return; + } + + [self setObject: value + forKey: key]; +} - (void)removeObjectForKey: (id)key { OF_UNRECOGNIZED_SELECTOR } Index: tests/OFDictionaryTests.m ================================================================== --- tests/OFDictionaryTests.m +++ tests/OFDictionaryTests.m @@ -17,10 +17,11 @@ #include "config.h" #import "OFDictionary.h" #import "OFString.h" #import "OFArray.h" +#import "OFNumber.h" #import "OFAutoreleasePool.h" #import "OFEnumerationMutationException.h" #import "TestsAppDelegate.h" @@ -44,18 +45,23 @@ OFEnumerator *key_enum, *obj_enum; OFArray *akeys, *avalues; [dict setObject: values[0] forKey: keys[0]]; - [dict setObject: values[1] - forKey: keys[1]]; + [dict setValue: values[1] + forKey: keys[1]]; TEST(@"-[objectForKey:]", [[dict objectForKey: keys[0]] isEqual: values[0]] && [[dict objectForKey: keys[1]] isEqual: values[1]] && [dict objectForKey: @"key3"] == nil) + TEST(@"-[valueForKey:]", + [[dict valueForKey: keys[0]] isEqual: values[0]] && + [[dict valueForKey: @"@count"] isEqual: + [OFNumber numberWithSize: 2]]) + TEST(@"-[containsObject:]", [dict containsObject: values[0]] && ![dict containsObject: @"nonexistant"]) TEST(@"-[containsObjectIdenticalTo:]",