Ios 按值搜索nsdictionary中的元素
我有一个nsdictionary,它包含具有以下结构的元素 名称-->值 电子邮件-->密钥 我从用户那里获得(上述结构的)价值, 现在,我想通过值(由用户输入)而不是键来搜索nsdictionary中的元素,无论它是否存在于nsdictionary中,并且还想获得该元素的索引(如果存在)Ios 按值搜索nsdictionary中的元素,ios,iphone,objective-c,nsdictionary,Ios,Iphone,Objective C,Nsdictionary,我有一个nsdictionary,它包含具有以下结构的元素 名称-->值 电子邮件-->密钥 我从用户那里获得(上述结构的)价值, 现在,我想通过值(由用户输入)而不是键来搜索nsdictionary中的元素,无论它是否存在于nsdictionary中,并且还想获得该元素的索引(如果存在) 如何做到这一点?字典数据结构没有“顺序”,因此您必须通过迭代集合并查找所需的值来搜索密钥 例如: NSString *targetKey = nil; NSArray *allKeys = [collecti
如何做到这一点?字典数据结构没有“顺序”,因此您必须通过迭代集合并查找所需的值来搜索密钥 例如:
NSString *targetKey = nil;
NSArray *allKeys = [collection allKeys];
for (int i = 0; i < [allKeys count]; ++i) {
NSString *key = [allKeys objectAtIndex:i];
NSString *obj = [collection objectForKey:key];
if ([obj isEqualToString:searchedString]) { // searchedString is what you're looking for
targetKey = key;
break;
}
}
// check if key was found (not nil) & proceed
// ...
NSString*targetKey=nil;
NSArray*allkey=[collection allkey];
对于(int i=0;i<[所有键计数];++i){
NSString*key=[allKeys objectAtIndex:i];
NSString*obj=[collection objectForKey:key];
如果([obj isEqualToString:searchedString]){//searchedString是您要查找的内容
targetKey=key;
打破
}
}
//检查是否找到钥匙(不是零)&继续
// ...
字典数据结构没有“顺序”,因此您必须通过迭代集合并查找所需的值来搜索密钥
例如:
NSString *targetKey = nil;
NSArray *allKeys = [collection allKeys];
for (int i = 0; i < [allKeys count]; ++i) {
NSString *key = [allKeys objectAtIndex:i];
NSString *obj = [collection objectForKey:key];
if ([obj isEqualToString:searchedString]) { // searchedString is what you're looking for
targetKey = key;
break;
}
}
// check if key was found (not nil) & proceed
// ...
NSString*targetKey=nil;
NSArray*allkey=[collection allkey];
对于(int i=0;i<[所有键计数];++i){
NSString*key=[allKeys objectAtIndex:i];
NSString*obj=[collection objectForKey:key];
如果([obj isEqualToString:searchedString]){//searchedString是您要查找的内容
targetKey=key;
打破
}
}
//检查是否找到钥匙(不是零)&继续
// ...
您可以在NSDictionary
中搜索输入的值,但无法获得值的索引,因为NSDictionary没有键值对的顺序
NSArray *array = [yourDictionaryObject allValues];
if ([array containsObject:@"userEnteredValue"]) {
<#statements#>
}
NSArray*array=[yourDictionaryObject allValues];
if([数组包含对象:@“userEnteredValue”]){
}
您可以在NSDictionary
中搜索输入的值,但无法获得值的索引,因为NSDictionary没有键值对的顺序
NSArray *array = [yourDictionaryObject allValues];
if ([array containsObject:@"userEnteredValue"]) {
<#statements#>
}
NSArray*array=[yourDictionaryObject allValues];
if([数组包含对象:@“userEnteredValue”]){
}
最好的方法是
-(NSArray*)allKeysForObject:(id)anObject
此
NSDictionary
方法将返回所有具有anObject
作为其值的键。如果每个对象在整个字典中只有一次,它将逻辑上返回一个数组,其中只有一个键 最好是
-(NSArray*)allKeysForObject:(id)anObject
此
NSDictionary
方法将返回所有具有anObject
作为其值的键。如果每个对象在整个字典中只有一次,它将逻辑上返回一个数组,其中只有一个键 您需要在字典中反复查找键
具有您需要的值:
试试这个:
NSArray *keys= [json allKeys];
for (NSString *keysV in keys){
NSLog(@"Keys are %@", keysV);
if([Your_Dict objectForKey: keysV] isEqual:@"string to Match"){
//Do your stuff here
}
}
您需要
在字典中反复查找键
具有您需要的值:
试试这个:
NSArray *keys= [json allKeys];
for (NSString *keysV in keys){
NSLog(@"Keys are %@", keysV);
if([Your_Dict objectForKey: keysV] isEqual:@"string to Match"){
//Do your stuff here
}
}
对于电子邮件的多个值,请在谓词中使用OR:
filter = [NSPredicate predicateWithFormat:@"contact_type = 42 OR contact_type = 23"];
对于电子邮件的多个值,请在谓词中使用OR:
filter = [NSPredicate predicateWithFormat:@"contact_type = 42 OR contact_type = 23"];
是的,这就是我要找的函数。是的,这就是我要找的函数。回答得好。节省了我的时间。谢谢你的回答。节省了我的时间。谢谢