如何从键值数组的字典数组中获取值。
-(void)viewDidLoad{
listMutableArray =[[NSMutableArray alloc] initWithObjects:[NSDictionary dictionaryWithObject:[NSArray
arrayWithObjects:@"kiran",@"1234512345",@"IN", nil] forKey:[NSArray
arrayWithObjects:@"name",@"phone",@"location", nil]],nil];
}
//将值设置为tableview单元格。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// Custom cell declaration completed.
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSLog(@"Value of name %@", [[listMutableArray objectAtIndex:indexPath.row] objectForKey:@"name"]);
}
从返回nil对象的可变数组中打印字典数组中的名称密钥对。
如何从可变数组中获取名称电话位置值。
答案 0 :(得分:2)
你的阵列形成错误像这样的表格数组
NSMutableArray *listMutableArray =[[NSMutableArray alloc] initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"kiran",@"name",@"1234512345",@"phone",@"IN",@"location", nil], nil];
NSLog(@"%@",listMutableArray);
并在cellForRowAtIndexPath
尝试打印
NSLog(@"Value of name %@", [[listMutableArray objectAtIndex:indexPath.row] objectForKey:@"name"]);
您想在数组流中添加多个Object以下代码
NSDictionary *dict = @{
@"name": @"kiran",
@"phone": @"1234512345",
@"location": @"IN"
};
NSDictionary *dict1 = @{
@"name": @"Bala",
@"phone": @"12345123",
@"location": @"OUT"
};
NSDictionary *dict2 = @{
@"name": @"Sri",
@"phone": @"898989898",
@"location": @"IN"
};
NSMutableArray *listMutableArray =[[NSMutableArray alloc] initWithObjects:dict,dict1,dict2, nil];
答案 1 :(得分:1)
您的代码正在创建一个包含单个字典的数组,其中数组作为键和值。我想你想要一系列字典;例如:
listMutableArray = [NSMutableArray new];
[listMutableArray addObject:@{
@"name": @"kiran",
@"phone": @"1234512345",
@"location": @"IN"
}];
答案 2 :(得分:0)
We get all NSDictionary response add in jsonResult(NSDictionary) and create globel (arrData)NSMutableArray.
arrData = [NSMutableArray new];
for (int i =0; i < [jsonResult count]; i++)
{
NSString *string = [NSString stringWithFormat:@"%d",i];
[arrData addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[[jsonResult valueForKey:string] valueForKey:@"name"],@"name",[[jsonResult valueForKey:string] valueForKey:@"phone"],@"phone",[[jsonResult valueForKey:string] valueForKey:@"location"],@"location",nil]];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// Custom cell declaration completed.
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSLog(@"Value of name %@", [[arrData objectAtIndex:indexPath.row] valueForKey:@"name"]);
return cell;
}
答案 3 :(得分:0)
NSArray *dictionary = @[
@{
@"Source_lat": @"28.6287",
@"Source_lon": @"77.3208",
@"Detail":@{
@"Address": @"NOIDA",
@"Region": @"NCR",
},
},
@{
@"Source_lat": @"28.628454",
@"Source_lon": @"77.376945",
}
];