ios - 从 ios 中的字典数组中的键获取数据

标签 ios objective-c nspredicate nsmutabledictionary

我有一个字典定义为

@property (retain, nonatomic) NSMutableDictionary *MyDictionary;

我试着得到这样的输出

NSLog(@"%@",MyDictionary);

这是我得到的输出

{ Category=(
        {
        code=12; Name="John Smith"
        },
        {
        code=21; Name="Bobby Smith"
        },
        {
        code=31; Name="Smith Jones"
        } );

Detail = {
        code=1;
        Text="Developer"
        };
}

我的表格单元格中填充了姓名,John Smith、Bobby Smith 等等。我需要的是在单击特定单元格时获取代码。例如,如果我点击名字 John Smith,我应该得到值 12

到目前为止,这是我的代码

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSArray *allNames = [MyDictionary allValues];
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"Name == %@", cell.textLabel.text];
    NSArray *filteredNames = [allNames filteredArrayUsingPredicate:resultPredicate];
    NSLog(@"%@",filteredNames);

}

filteredNames 不包含任何过滤的对象。如何获取代码的值?

最佳答案

编辑:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
   NSString *getCodeValue = [[[myDic objectForKey:@"Category"] objectAtIndex:indexPath.row] objectForKey:@"code"]
   NSLog (@"%@", getCodeValue)      
}

didSelectRowAtIndexPath 方法中使用此代码

关于ios - 从 ios 中的字典数组中的键获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18572872/

相关文章:

objective-c - GPS 航向 iOS

ios - 使用 Swift 的位置 BLE GPS 解码 location_and_speed

Objective-c 优先级队列

iphone - 在保持旋转的情况下缩放 UIScrollView 内的 UIImageView

ios - 核心数据 : NSPredicate one to many to many relations

ios - 为什么在将 objectWithID 与 CoreData 一起使用时我没有得到相同的对象?

ios - 如何在 stackview 中添加 uilabel 左侧的 uiimage?

ios - FBSDKLoginBehaviorSystemAccount 不工作

ios - 如何处理 ABPeoplePickerNavigationController 中的属性选择?

objective-c - 使用 NSPredicate 的 NSArray 过滤器,比较日期