简体   繁体   English

如何从mutableArray访问数组字典值

[英]How to access the array dictionary values from mutableArray

how to get value from array of dictionary of key value array. 如何从键值数组的字典数组中获取值。

-(void)viewDidLoad{
listMutableArray =[[NSMutableArray alloc] initWithObjects:[NSDictionary dictionaryWithObject:[NSArray 
arrayWithObjects:@"kiran",@"1234512345",@"IN", nil] forKey:[NSArray 
arrayWithObjects:@"name",@"phone",@"location", nil]],nil];
}

// set value to tableview cell. //将值设置为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"]);
}

when print the name key pair from the dictionary array from the mutable array which return nil object. 当从返回nil对象的可变数组的字典数组中打印名称密钥对时。

how to get name phone location values from the mutable array. 如何从可变数组获取名称电话位置值。

Your array formation is wrong Form array like this 您的数组格式错误这样的Form数组

NSMutableArray *listMutableArray =[[NSMutableArray alloc] initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"kiran",@"name",@"1234512345",@"phone",@"IN",@"location", nil], nil];

NSLog(@"%@",listMutableArray);

And In cellForRowAtIndexPath try to print 并在cellForRowAtIndexPath尝试打印

NSLog(@"Value of name %@", [[listMutableArray objectAtIndex:indexPath.row] objectForKey:@"name"]);

You want to add multiple Object in array flow the below code 您想在数组流中添加多个对象,如下代码

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];

Your code is creating an array containing a single dictionary with an array as a key and value. 您的代码正在创建一个包含单个字典的数组,该字典具有作为键和值的数组。 I think you want an array of dictionaries; 我认为您想要一系列词典; for example: 例如:

listMutableArray = [NSMutableArray new];
[listMutableArray addObject:@{
    @"name": @"kiran",
    @"phone": @"1234512345",
    @"location": @"IN"
}];
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;
}
NSArray *dictionary = @[
                              @{
                                @"Source_lat": @"28.6287",
                                @"Source_lon": @"77.3208",
                                @"Detail":@{
                                            @"Address": @"NOIDA",
                                            @"Region": @"NCR",
                                           },
                                },
                              @{
                                  @"Source_lat": @"28.628454",
                                  @"Source_lon": @"77.376945",

                                  }

                             ];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM