4

我有一个字典数组,我想通过它找到一个匹配的值,然后我可以 Count == 数组的第 N 个项目

数组的每个项目看起来像这样

HMOD = 0;
MID = 39;
MOD = SOMETHING; // looking for this value
ID = 50;

所以我想创建一个循环遍历数组,直到找到匹配的值,然后我使用计数中的数字作为对下一个视图中索引路径的引用。

我已经编写了这段代码,它无法正常工作......但希望它能让您了解我正在尝试创建的循环。

int count = 0;
while (singleName != [[ModArray valueForKey:@"MOD"] objectAtIndex:count]) {
                    count ++;
                    NSLog(@"%i", count);
                }

SingleName 是一个 NSString,我用来匹配 ModArray 中的 MOD 值...任何帮助将不胜感激。

4

3 回答 3

10

这是通过valueForKey在字典数组上使用的更简单的解决方案,

假设你modArray是这样的,

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

并且singleName有一个值"something"

    NSString *singleName = @"something";

array从其中获取modArray具有键为的对象"MOD"

    NSArray *array = [modArray valueForKey:@"MOD"];

检查是否singleName存在于 this 中array。如果是,则获取该对象的第一个索引,该索引与键为 " MOD" in的字典的索引相同modArray

    if ([array containsObject:singleName]) {
        NSLog(@"%d", [array indexOfObject:singleName]);
    } else {
        NSLog(@"%@ is not present in the array", singleName);
    }

更新: 如果您想以自己的方式进行操作,那么您使用的唯一错误是!=您应该使用isEqualToString. 你应该这样做,

int count = 0;
while (![singleName isEqualToString:[[modArray valueForKey:@"MOD"] objectAtIndex:count]]) {
                    count ++;
                    NSLog(@"%i", count);
                }
于 2012-10-31T05:07:44.050 回答
3

您的代码从里到外看起来。你说你有一系列字典。假设ModArray是数组(基于名称),您可能会这样做:

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

编辑:基于 ACB 纠正我对 的误解NSArray valueForKey:,唯一真正的问题是您使用 != 比较两个字符串。

于 2012-10-31T03:56:15.907 回答
3
- 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];
    }
于 2015-12-17T11:55:20.930 回答