ObjFW  Check-in [f816d1ec7c]

Overview
Comment:Move -[setValue:forKey:] to OFDictionary

If this is only overridden in OFMutableDictionary, this would lead to
inconsistent behavior, as it could be called on an OFDictionary as well
due to the fact that -[setValue:forKey:] is defined in OFObject.
Instead, if the dictionary is immutable, an OFUndefinedKeyException is
thrown.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f816d1ec7c4080a64a6a63aa96a31a5346292aecb3719ad0b5f051bd450e5a9e
User & Date: js on 2016-06-05 15:11:04
Other Links: manifest | tags
Context
2016-06-05
15:50
Fix OFDictionaryTests check-in: 4ad79a7f67 user: js tags: trunk
15:11
Move -[setValue:forKey:] to OFDictionary check-in: f816d1ec7c user: js tags: trunk
15:00
Implement Key Value Coding for OFArray check-in: 05196e511a user: js tags: trunk
Changes

Modified src/OFDictionary.h from [2d197e0288] to [dff64e5368].

208
209
210
211
212
213
214















215
216
217
218
219
220
221
 * @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
 * @return A boolean whether the dictionary contains the specified object
 */







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
 * @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 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:. In this case, if the dictionary is immutable, an
 * @ref OFUndefinedKeyException is thrown.
 *
 * @param key The key to set
 * @param value The value to set the key to
 */
- (void)setValue: (nullable id)value
	  forKey: (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
 * @return A boolean whether the dictionary contains the specified object
 */

Modified src/OFDictionary.m from [3204259101] to [034710c781].

25
26
27
28
29
30
31

32
33
34
35
36
37
38
#import "OFArray.h"
#import "OFString.h"
#import "OFXMLElement.h"
#import "OFDataArray.h"

#import "OFInvalidArgumentException.h"
#import "OFOutOfRangeException.h"


static struct {
	Class isa;
} placeholder;

@interface OFDictionary ()
- (OFString*)OF_JSONRepresentationWithOptions: (int)options







>







25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#import "OFArray.h"
#import "OFString.h"
#import "OFXMLElement.h"
#import "OFDataArray.h"

#import "OFInvalidArgumentException.h"
#import "OFOutOfRangeException.h"
#import "OFUndefinedKeyException.h"

static struct {
	Class isa;
} placeholder;

@interface OFDictionary ()
- (OFString*)OF_JSONRepresentationWithOptions: (int)options
294
295
296
297
298
299
300























301
302
303
304
305
306
307
		objc_autoreleasePoolPop(pool);

		return [ret autorelease];
	}

	return [self objectForKey: key];
}
























- (size_t)count
{
	OF_UNRECOGNIZED_SELECTOR
}

- copy







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
		objc_autoreleasePoolPop(pool);

		return [ret autorelease];
	}

	return [self objectForKey: 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;
	}

	if (![self isKindOfClass: [OFMutableDictionary class]])
		@throw [OFUndefinedKeyException exceptionWithObject: self
								key: key
							      value: value];

	[(OFMutableDictionary*)self setObject: value
				       forKey: key];
}

- (size_t)count
{
	OF_UNRECOGNIZED_SELECTOR
}

- copy

Modified src/OFMutableDictionary.h from [549070ec8e] to [d87ccc345a].

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
 * @param object The object to set the key to
 */
- (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
 */
- (void)removeObjectForKey: (KeyType)key;








<
<
<
<
<
<
<
<
<
<
<
<
<
<







73
74
75
76
77
78
79














80
81
82
83
84
85
86
 * @param object The object to set the key to
 */
- (void)setObject: (ObjectType)object
	   forKey: (KeyType)key;
-   (void)setObject: (ObjectType)object
  forKeyedSubscript: (KeyType)key;















/*!
 * @brief Removes the object for the specified key from the dictionary.
 *
 * @param key The key whose object should be removed
 */
- (void)removeObjectForKey: (KeyType)key;

Modified src/OFMutableDictionary.m from [87db87aaf2] to [b571ce2a18].

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
-   (void)setObject: (id)object
  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
}

- (void)removeAllObjects
{







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







173
174
175
176
177
178
179


















180
181
182
183
184
185
186
-   (void)setObject: (id)object
  forKeyedSubscript: (id)key
{
	[self setObject: object
		 forKey: key];
}



















- (void)removeObjectForKey: (id)key
{
	OF_UNRECOGNIZED_SELECTOR
}

- (void)removeAllObjects
{

Modified tests/OFDictionaryTests.m from [b3dcaccc4e] to [c47931a3b5].

19
20
21
22
23
24
25

26
27
28
29
30
31
32
#import "OFDictionary.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFNumber.h"
#import "OFAutoreleasePool.h"

#import "OFEnumerationMutationException.h"


#import "TestsAppDelegate.h"

static OFString *module = @"OFDictionary";
static OFString *keys[] = {
	@"key1",
	@"key2"







>







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#import "OFDictionary.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFNumber.h"
#import "OFAutoreleasePool.h"

#import "OFEnumerationMutationException.h"
#import "OFUndefinedKeyException.h"

#import "TestsAppDelegate.h"

static OFString *module = @"OFDictionary";
static OFString *keys[] = {
	@"key1",
	@"key2"
55
56
57
58
59
60
61




62
63
64
65
66
67
68
	    [[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:]",
	    [dict containsObjectIdenticalTo: values[0]] &&







>
>
>
>







56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	    [[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]])

	EXPECT_EXCEPTION(@"Catching -[setValue:forKey:] on immutable "
	    @"dictionary", OFUndefinedKeyException, [dict setValue: @"x"
							    forKey: @"x"])

	TEST(@"-[containsObject:]",
	    [dict containsObject: values[0]] &&
	    ![dict containsObject: @"nonexistant"])

	TEST(@"-[containsObjectIdenticalTo:]",
	    [dict containsObjectIdenticalTo: values[0]] &&