我必须存储属性数组:
| attr-1 | attr-2 | ...... | attr-n
---------------------------------------
A | val-A1 | val-A2 | ...... | val-An
---------------------------------------
B | val-B1 | val-B2 | ...... | val-Bn现在,我就是这样做的:
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
NSDictionary *A =
[NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:val-A1,
val-A2,
......,
val-An, nil]
forKeys:
[NSArray arrayWithObjects:@"attr-1",
@"attr-2",
.........,
@"attr-n", nil]];
NSDictionary *B =
[NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:val-B1,
val-B2,
......,
val-Bn, nil]
forKeys:
[NSArray arrayWithObjects:@"attr-1",
@"attr-2",
.........,
@"attr-n", nil]];
[attributes setObject:A forKey:@"A"];
[attributes setObject:B forKey:@"B"];这是大量的编码工作。有谁知道更好的解决方案吗?
PS。价值观不会改变。
发布于 2015-03-17 15:47:53
可能是这样的东西
NSDictionary *A = @{ @"attr-1" : @1, @"attr-2" : @2 };
NSDictionary *B = @{ @"attr-1" : @4, @"attr-2" : @5 };
NSMutableDictionary *attributes = [@{ @"A" : A, @"B" : B } mutableCopy];然后,您可以使用如下代码检索5
NSNumber *number = attributes[@"B"][@"attr-2"];
NSLog( @"%d", number.intValue );https://stackoverflow.com/questions/29093544
复制相似问题