HelenaM
HelenaM

Reputation: 1855

How can generate an NSMutableDictionary with multiple keys and values

I want to generate a nsmutmutable dictionary with the following structure:

"user1": {
            "phone Number": "123-123-1234"
            "email": "[email protected]"
            "title": "sales"
            }
"user1": {
            "phone Number": "123-123-1234"
            "email": "[email protected]"
            "title": "support"
            }
"user2": {
            "phone Number": "123-123-1234"
            "email": "[email protected]"
            "title": "management"
            } 

any of you knows how can I do this?

Upvotes: 0

Views: 79

Answers (2)

Melvin Sovereign
Melvin Sovereign

Reputation: 445

You could just define a 'MyContact' object. Since you want a 'structure', seems like that would do what you want unless I'm missing something:

@interface MyContact : NSObject {

@property (retain) PhoneNumber;
@property (retain) Email;

- (id) initWithPhone: phone_ Email: _email;
}

...then

NSMutableDictionary *people = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
   [MyContact initWithPhone: some_number Email: some_email] , @"user1", 
   [MyContact initWithPhone: other_number Email: other_email], @"user2", nil ] ;

Upvotes: 0

TomSwift
TomSwift

Reputation: 39502

Something like this:

    NSMutableDictionary* md = [@{   @"user1" : @{   @"phone Number" : @"123-123-1234",
                                                    @"email" : @"[email protected]",
                                                    @"title" : @"sales" },

                                    @"user2" : @{   @"phone Number" : @"123-123-1234",
                                                    @"email" : @"[email protected]",
                                                    @"title" : @"support" },

                                    @"user3" : @{   @"phone Number" : @"123-123-1234",
                                                    @"email" : @"[email protected]",
                                                    @"title" : @"management" }

                                                   } mutableCopy];

or:

    NSMutableDictionary* md2 = [NSMutableDictionary dictionaryWithDictionary:
                                @{  @"user1" : @{   @"phone Number" : @"123-123-1234",
                                                    @"email" : @"[email protected]",
                                                    @"title" : @"sales" },

                                    @"user2" : @{   @"phone Number" : @"123-123-1234",
                                                    @"email" : @"[email protected]",
                                                    @"title" : @"support" },

                                    @"user3" : @{   @"phone Number" : @"123-123-1234",
                                                    @"email" : @"[email protected]",
                                                    @"title" : @"management" }
                                } ];

If you want each inner dictionary to be mutable you can apply the same pattern:

    NSMutableDictionary* md3 = [@{  @"user1" : [@{   @"phone Number" : @"123-123-1234",
                                                    @"email" : @"[email protected]",
                                                    @"title" : @"sales" } mutableCopy],

                                    @"user2" : [@{   @"phone Number" : @"123-123-1234",
                                                    @"email" : @"[email protected]",
                                                    @"title" : @"support" } mutableCopy],

                                    @"user3" : [@{   @"phone Number" : @"123-123-1234",
                                                    @"email" : @"[email protected]",
                                                    @"title" : @"management" } mutableCopy]

                               } mutableCopy];

Upvotes: 2

Related Questions