使用字符串访问字典的值

发布于 2024-11-02 08:56:31 字数 1383 浏览 1 评论 0原文

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

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

通过将字符串与字典中的键进行比较,我需要检索 person 对象的内容。我该怎么做?请帮我。我的代码是:

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 等。

I have an NSMutableDictionary with three key/value pairs.

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

Please help me with some hints. I want to access the properties, GivenName, Surname, etc., in the person object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白昼 2024-11-09 08:56:31

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

aPerson = [personDict objectForKey: agentrefattr];

The keys in a dictionary are unique. So if agentrefattr is a string representing a key, yuu will only ever have one object in the dictionary with that key, so there is no need for a loop at all. Just do this:

aPerson = [personDict objectForKey: agentrefattr];
慕巷 2024-11-09 08:56:31

更好的设计是使用 enumerateobjectsandkeyswithoption:使用 block:^(id obj,id key)

A much better design is using enumerateobjectsandkeyswithoption: using block:^(id obj,id key)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文