生成这个字符串的最好方法是什么?(NSMutableString…)

时间:2022-06-29 20:08:23

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

我有一个字典,它的键是nsstring,对象是NSArray。这里有一个例子:

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.

我将把这个字符串输入一个NSPredicate。生成这个字符串的最好方法是什么?我可以用for循环,if,等等,但是有没有更优雅的方法呢?谢谢。

2 个解决方案

#1


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

这不是一个有效的谓词格式字符串,因此即使您最终生成它,您也无法将它转换为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];

#2


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

#1


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

这不是一个有效的谓词格式字符串,因此即使您最终生成它,您也无法将它转换为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];

#2


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