Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static NSMutableDictionary

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];
    }
}
like image 714
Idrees Ashraf Avatar asked Jul 04 '12 12:07

Idrees Ashraf


People also ask

What is NSMutableDictionary in Swift?

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 .

How do you convert NSDictionary to NSMutableDictionary?

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.

Is NSDictionary thread safe?

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.

What is NSDictionary in Objective c?

An object representing a static collection of key-value pairs, for use instead of a Dictionary constant in cases that require reference semantics.


1 Answers

This is the right way to do that. Just bear in mind that the dictionary won't get deallocated at any point.

like image 93
pzearfoss Avatar answered Sep 28 '22 02:09

pzearfoss