Reputation: 5510
I have an array of dictionaries that I would like to go through to find a matching value that I can then Count == Nth item of the array
Each item of the array looks like this
HMOD = 0;
MID = 39;
MOD = SOMETHING; // looking for this value
ID = 50;
So I would like to create a loop that goes through the array until it finds the matching value, then I use the number in the count as an reference to Index path in the next view..
I have written this peice of code which dosnt work... but hopefully it gives you an idea of the loop I am trying to create.
int count = 0;
while (singleName != [[ModArray valueForKey:@"MOD"] objectAtIndex:count]) {
count ++;
NSLog(@"%i", count);
}
SingleName is a NSString that I am using to match the MOD value in ModArray... Any help would be greatly appreciated.
Upvotes: 4
Views: 13423
Reputation: 780
- This is Helpful when you search from Dictionary.
NSMutableArray *contentList;
NSMutableArray *filteredContentList;
BOOL isSearching;
// firstSection is array which already filled.
// contentList array for value of particular key
// filteredContentList is search array from actual array.
- (void)searchTableList {
NSString *searchString = searchBar.text;
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"frame_code beginswith[c] %@", searchString];
NSArray *filteredArr = [firstSection filteredArrayUsingPredicate:filterPredicate];
if(contentList.count > 0)
[contentList removeAllObjects];
[filteredContentList addObjectsFromArray:filteredArr];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar1 {
if ([searchBar1.text length] != 0)
isSearching = YES;
else
isSearching = NO;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(@"Text change - %d",isSearching);
//Remove all objects first.
[filteredContentList removeAllObjects];
if([searchText length] != 0) {
isSearching = YES;
[self searchTableList];
}
else {
isSearching = NO;
}
[tblFrameList_SComplete reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(@"Cancel clicked");
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(@"Search Clicked");
[self searchTableList];
}
Upvotes: 3
Reputation: 318944
Your code looks all inside out. You state you have an array of dictionaries. Assuming ModArray
is the array (based on the name) you might do this:
NSUInteger count = 0;
for (NSDictionary *dict in ModArray) { // iterate through the array
NSString *mod = dict[@"MOD"]; // get the value for MOD
if ([mod isEqualToString:singleName]) { // compare the two strings
break; // they match so exit the loop
}
count++
}
// count has the index of the dictionary with the matching MOD value
Edit: Based on ACB correcting my misunderstanding of NSArray valueForKey:
, the only real issue is your use of using != to compare the two strings.
Upvotes: 3
Reputation: 23278
Here is a simpler solution by using valueForKey
on the array of dictionaries,
Assuming that your modArray
is like this,
NSArray *modArray = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObject:@"0" forKey:@"HMOD"],
[NSDictionary dictionaryWithObject:@"39" forKey:@"MID"],
[NSDictionary dictionaryWithObject:@"something" forKey:@"MOD"],
[NSDictionary dictionaryWithObject:@"50" forKey:@"ID"], nil];
And singleName
has a value as "something"
NSString *singleName = @"something";
Fetch the array
from modArray
which has an object with key as "MOD"
,
NSArray *array = [modArray valueForKey:@"MOD"];
Check if singleName
is present in this array
. If yes, then get the first index of that object which will be same as the index of dictionary with key "MOD
" in modArray
.
if ([array containsObject:singleName]) {
NSLog(@"%d", [array indexOfObject:singleName]);
} else {
NSLog(@"%@ is not present in the array", singleName);
}
Update:
If you want to do it in your way, only mistake was you were using !=
whereas you should have used isEqualToString
. You should have done like this,
int count = 0;
while (![singleName isEqualToString:[[modArray valueForKey:@"MOD"] objectAtIndex:count]]) {
count ++;
NSLog(@"%i", count);
}
Upvotes: 10