3

I have a dictionary whose keys are NSStrings and whose objects are NSArray. Here's an example:

key (NSString) : GroupA 
value (NSArray): John
                 Alex
                 Joe
                 Bob

There are many entries like this, this is just an example. What I need to do is generate a string like this (for example:

(GroupA contains[cd] ('John' OR 'Alex' OR 'Joe' OR 'Bob')) AND (GroupB contains[cd] ('Starcraft' OR 'WOW' OR 'Warcraft' OR 'Diablo')) AND ..... 

I am going to be feeding this string to an NSPredicate. What's the best way to generate this string? I can use for loops and if and all that, but is there a much more elegant way? Thanks.

user635064
  • 6,219
  • 12
  • 54
  • 100

2 Answers2

5

That's not a valid predicate format string, so even if you end up generating it, you won't be able to convert it into an NSPredicate

Here's what you want instead:

NSDictionary *groupValuePairs = ....;

NSMutableArray *subpredicates = [NSMutableArray array];
for (NSString *group in groupValuePairs) {
  NSArray *values = [groupValuePairs objectForKey:group];
  NSPredicate *p = [NSPredicate predicateWithFormat:@"%K IN %@", group, values];
  [subpredicates addObject:p];
}

NSPredicate *final = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];

This doesn't have the case- and diacritic-insensitivity that your original was implying. If you really need that, then you're going to need to get a bit more complicated:

NSDictionary *groupValuePairs = ....;

NSMutableArray *subpredicates = [NSMutableArray array];
for (NSString *group in groupValuePairs) {
  NSArray *values = [groupValuePairs objectForKey:group];
  NSMutableArray *groupSubpredicates = [NSMutableArray array];
  for (NSString *value in values) {
      NSPredicate *p = [NSPredicate predicateWithFormat:@"%K contains[cd] %@", group, value];
      [groupSubpredicates addObject:p];
  }
  NSPredicate *p = [NSCompoundPredicate orPredicateWithSubpredicates:groupSubpredicates];
  [subpredicates addObject:p];
}

NSPredicate *final = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
1

It should be something like this

NSDictionary *myDict = [NSDictionary dictionaryWithObject:[NSArray arrayWithObjects:@"John",@"Alex",@"Joe",@"Bob",nil] forKey:@"GroupA"];
NSString *myString = @"(";

int j = 0;
for(NSString *key in [myDict allKeys]) {
    NSString *value = [myDict valueForKey:key];
    myString = [myString stringByAppendingFormat:@"%@ contains[cd] (", key];
    NSArray *myArray = (NSArray *)value;

    int idx = 0;
    for(NSString *name in myArray) {
        myString = [myString stringByAppendingFormat:@"'%@'",name];
        if(idx < [myArray count] - 1) {
            myString = [myString stringByAppendingFormat:@" OR "];
        } 
        idx++;
    }

    myString = [myString stringByAppendingString:@")"];

    if(j < [myDict count] -1) {
        myString = [myString stringByAppendingString:@" AND "];
    }

    j++;

};

myString = [myString stringByAppendingString:@")"];

NSLog(@"mystring %@",myString);
brandontreb
  • 397
  • 2
  • 12
  • 2
    What if `name` contains an apostrophe, e.g. O'Hara? –  Jun 02 '11 at 21:59
  • Really? So why just downvote my answer without providing one of your own? #trollin – brandontreb Jun 02 '11 at 22:02
  • 1
    This isn't a *bad* approach (though there are problems with it). I'd recommend using an `NSMutableString` though. You'll have **far** fewer temporary objects that way (and thus lower memory usage). – Dave DeLong Jun 02 '11 at 22:07
  • 1
    @brandontreb Well, at least you should know about the method of `NSArray -componentsJoinedByString:`. This way you will get much more maintainable code. And I don't quite understand the purpose of using fast enumeration when you still create and increment your own iteration indexes. – Stanislav Yaglo Jun 02 '11 at 22:14
  • @stam good call. That would have made a lot more sense. As far as incrementing counters, I was going to use blocks for iteration, but I wanted to keep the answer a little more "simple". – brandontreb Jun 03 '11 at 01:43