I want to use static NSMutableDictionary. Can you please help how to use static NSMutableDictionary in class.
Is it right way? .h file
+(NSMutableDictionary*)contactsToAssignBill;
+(void)setContactsToAssignBill:(NSMutableDictionary*)value;
.m file
static NSMutableDictionary * contactsToAssignBill;
+(NSMutableDictionary*)contactsToAssignBill
{
if (!contactsToAssignBill)
contactsToAssignBill = [[NSMutableDictionary alloc] init];
return contactsToAssignBill;
}
+(void)setContactsToAssignBill:(NSMutableDictionary *)value
{
if(contactsToAssignBill != value)
{
[contactsToAssignBill release];
contactsToAssignBill = [value mutableCopy];
}
}
The NSMutableDictionary class declares the programmatic interface to objects that manage mutable associations of keys and values. It adds modification operations to the basic operations it inherits from NSDictionary . NSMutableDictionary is “toll-free bridged” with its Core Foundation counterpart, CFMutableDictionary .
Use -mutableCopy . NSDictionary *d; NSMutableDictionary *m = [d mutableCopy]; Note that -mutableCopy returns id ( Any in Swift) so you will want to assign / cast to the right type. It creates a shallow copy of the original dictionary.
In general, the collection classes (for example, NSMutableArray , NSMutableDictionary ) are not thread-safe when mutations are concerned. That is, if one or more threads are changing the same array, problems can occur.
An object representing a static collection of key-value pairs, for use instead of a Dictionary constant in cases that require reference semantics.
This is the right way to do that. Just bear in mind that the dictionary won't get deallocated at any point.
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