生成此字符串的最佳方法是什么? (NSMutableString...)

发布于 2024-11-13 06:08:41 字数 489 浏览 0 评论 0原文

我有一个字典,其键是 NSStrings,其对象是 NSArray。举个例子:

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

类似这样的条目还有很多,这只是一个例子。我需要做的是生成一个像这样的字符串(例如:

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

我将把这个字符串提供给 NSPredicate。生成这个字符串的最佳方法是什么?我可以使用 for 循环和 if 等等,但是有吗?更优雅的方式? 谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱*していゐ 2024-11-20 06:08:41

这不是一个有效的谓词格式字符串,因此即使您最终生成了它,您也无法将其转换为 NSPredicate

这就是您想要的:

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

这没有这种情况 -以及您的原文所暗示的变音符号不敏感。如果你真的需要它,那么你需要变得更复杂一些:

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

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];
神回复 2024-11-20 06:08:41

应该是这样的

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文