objective-c - 使用字符串访问字典的值

标签 objective-c cocoa nsmutabledictionary

我有一个带有三个键/值对的NSMutableDictionary

key1 - person object1 
key2 - person object2 
key3 - person object3

By comparing a string with the key in the dictionary, I need to retrieve the contents of the person object. How can I do this? Please help me. My code is:

NSArray *keys = [persondict allKeys];
    id key;
    for(int i= 0; i<[keys count]; ++i)
    {   key = [keys objectAtIndex:i];
    }
    for(id key in persondict){
        if ([key isEqual:agentrefattr]){
            //how to get the person object here?
            [aperson setPhoneNumber:aperson.PhoneNumber];
            [aperson setEmailAddress:aperson.EmailAddress];
            [aPersonName setGivenName:aPersonName.GivenName];
            [aPersonName setSurname:aPersonName.Surname];
            [aperson setPersonName:aperson.PersonName];

            [self.agentarray insertObject:aperson atIndex:index];
            [self.agentnamearray insertObject:aPersonName atIndex:index];
            aperson = [agentarray objectAtIndex:index];
            aPersonName = [agentnamearray objectAtIndex:index];

            NSLog(@"att:%@",agentrefattr);
            NSLog(@"Email :%@",aperson.EmailAddress);
            NSLog(@"Phone :%@",aperson.PhoneNumber);
            NSLog(@"Given Name :%@",aperson.PersonName.GivenName);
            NSLog(@"SurName :%@",aperson.PersonName.Surname);

请帮我一些提示。我想访问 person 对象中的属性,GivenNameSurname 等。

最佳答案

字典中的键是唯一的。因此,如果 agentrefattr 是代表某个键的字符串,则 yuu 在字典中只会有一个具有该键的对象,因此根本不需要循环。只需这样做:

aPerson = [personDict objectForKey: agentrefattr];

关于objective-c - 使用字符串访问字典的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5732027/

相关文章:

objective-c - 过度使用 NSView -> 替代方案?

cocoa - 在核心动画中,如何获取图层框架和旋转?

ios - 当我有多个 iOS 部分时从 UITableView 中删除行(值在 Dynamic NSMutableDictionary 中)

objective-c - iOS5、UIScrollView 和 layoutSubviews 行为

iphone - 在底部加载最近的消息并放置一个加载旧消息按钮以在 TableView 上加载旧消息

ios - 无法读取 objective-c 中json文件的具体值

swift - 在 Swift 中从 Cocoa App 运行终端命令失败 : "command not found" but works using Command Line Tool in swift

iphone - NSMutableDictionary SetObject :ForKey: is not inserting NSDictionary object

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

objective-c - 如何在Mac上以编程方式更改光标大小