There is a literal syntax to add object and change object in an NSMutableDictionary, is there a literal syntax to remove object?
Just like with NSString s, collection objects made via literal arrays and dictionaries are immutable. You cannot create mutable collections with the literal syntax, so you will have to make a mutable copy after making the immutable dictionary or array.
An object representing a static collection of key-value pairs, for use instead of a Dictionary constant in cases that require reference semantics.
In Objective-C, any character, numeric or boolean literal prefixed with the '@' character will evaluate to a pointer to an NSNumber object initialized with that value. C's type suffixes may be used to control the size of numeric literals.
Yes, but... :-)
This is not supported by default, however the new syntax for setting dictionary elements uses the method setObject:forKeyedSubscript:
rather than setObject:forKey:
. So you can write a category which replaces the former and either sets or removes the element:
@implementation NSMutableDictionary (RemoveWithNil)
- (void) setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key
{
if (obj)
[self setObject:obj forKey:key];
else
[self removeObjectForKey:key];
}
@end
Add that to your application and then:
dict[aKey] = nil;
will remove an element.
No. There is not. I have tried to find proof link but did not succeed :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With