使用字符串访问字典的值
我有一个带有三个键/值对的 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 对象中的属性,GivenName
、Surname
等。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
字典中的键是唯一的。因此,如果
agentrefattr
是代表某个键的字符串,则 yuu 在字典中只会有一个具有该键的对象,因此根本不需要循环。只需这样做: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:更好的设计是使用 enumerateobjectsandkeyswithoption:使用 block:^(id obj,id key)
A much better design is using enumerateobjectsandkeyswithoption: using block:^(id obj,id key)