簡體   English   中英

如何從mutableArray訪問數組字典值

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

如何從鍵值數組的字典數組中獲取值。

-(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對象的可變數組的字典數組中打印名稱密鑰對時。

如何從可變數組獲取名稱電話位置值。

您的數組格式錯誤這樣的Form數組

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

您想在數組流中添加多個對象,如下代碼

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

您的代碼正在創建一個包含單個字典的數組,該字典具有作為鍵和值的數組。 我認為您想要一系列詞典; 例如:

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