从 NSDictionary 获取单独的值

Fetching separate values from NSDictionary

我已将我的数据映射到 NSDictionary 中。数据一键多值映射

 NSMutableDictionary *dict = [[NSMutableDictionary alloc]init]; 
     int count = 1; 
     int intval=111; 
     int intval2 = 222; 
     [dict setObject:[NSString stringWithFormat:@"%d,%d",intval,intval2]
     forKey@"%d",count];
     count++;

如何获取一个键的两个整数值,例如 key=1?我需要在整数变量中分别获取值 111,222。

首先你不能将对象(此方法用于NSMutableArray)添加到字典中,你可以为任何键设置对象或设置值。

如果您要插入与上面相同的记录并且有两个整数仅用逗号分隔,那么您可以使用以下方式获取它:

NSString *myBothvalue = [dict valueForKey:@"count"];
NSArray *temp = [myBothvalue componentsSeparatedByString:@","];

NSInteger value1 = [[temp objectAtIndex:0] integerValue];
NSInteger value2 = [[temp objectAtIndex:1] integerValue];

希望对您有所帮助。

//set object
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
int count = 1; int intval=111; int intval2 = 222;
[dict setObject:[NSString stringWithFormat:@"%d,%d",intval,intval2] forKey:@"count"];
count++;

//Read from dictionary
NSArray *arrayofCount=[[dict valueForKey:@"count"]componentsSeparatedByString:@","];

if(arrayofCount.count>0)
{
    int readintval = [[arrayofCount objectAtIndex:0] intValue];
}
if(arrayofCount.count>1)
{
    int readintval2 = [[arrayofCount objectAtIndex:1] intValue];
}