我有一个带有三个键/值对的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 对象中的属性,GivenName
、Surname
等。
最佳答案
字典中的键是唯一的。因此,如果 agentrefattr
是代表某个键的字符串,则 yuu 在字典中只会有一个具有该键的对象,因此根本不需要循环。只需这样做:
aPerson = [personDict objectForKey: agentrefattr];
关于objective-c - 使用字符串访问字典的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5732027/