0

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

"user1": {
            "phone Number": "123-123-1234"
            "email": "me@me.com"
            "title": "sales"
            }
"user1": {
            "phone Number": "123-123-1234"
            "email": "me3@me.com"
            "title": "support"
            }
"user2": {
            "phone Number": "123-123-1234"
            "email": "me3@me.com"
            "title": "management"
            } 

any of you knows how can I do this?

4

2 回答 2

2

像这样的东西:

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

                                    @"user2" : @{   @"phone Number" : @"123-123-1234",
                                                    @"email" : @"me2@me.com",
                                                    @"title" : @"support" },

                                    @"user3" : @{   @"phone Number" : @"123-123-1234",
                                                    @"email" : @"me3@me.com",
                                                    @"title" : @"management" }

                                                   } mutableCopy];

或者:

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

                                    @"user2" : @{   @"phone Number" : @"123-123-1234",
                                                    @"email" : @"me2@me.com",
                                                    @"title" : @"support" },

                                    @"user3" : @{   @"phone Number" : @"123-123-1234",
                                                    @"email" : @"me3@me.com",
                                                    @"title" : @"management" }
                                } ];

如果您希望每个内部字典是可变的,您可以应用相同的模式:

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

                                    @"user2" : [@{   @"phone Number" : @"123-123-1234",
                                                    @"email" : @"me2@me.com",
                                                    @"title" : @"support" } mutableCopy],

                                    @"user3" : [@{   @"phone Number" : @"123-123-1234",
                                                    @"email" : @"me3@me.com",
                                                    @"title" : @"management" } mutableCopy]

                               } mutableCopy];
于 2013-06-24T20:42:50.053 回答
0

您可以只定义一个“MyContact”对象。既然你想要一个“结构”,除非我遗漏了一些东西,否则它似乎会做你想做的事:

@interface MyContact : NSObject {

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

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

...然后

NSMutableDictionary *people = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
   [MyContact initWithPhone: some_number Email: some_email] , @"user1", 
   [MyContact initWithPhone: other_number Email: other_email], @"user2", nil ] ;
于 2013-06-24T22:37:03.613 回答