我想生成一个具有以下结构的nsmutmutable字典:
"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"
}
你们中的任何人都知道我该怎么做?
答案 0 :(得分: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];
答案 1 :(得分: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 ] ;