【问题标题】:Searching an array for an object and checking if a NSString field matches在数组中搜索对象并检查 NSString 字段是否匹配
【发布时间】:2015-02-23 09:38:11
【问题描述】:

我有一个 NSMutableArray 的朋友,其中包含 Parse.com PFUser 对象。每个 PFUser 都有一个 NSString 字段。我想在这个数组中搜索包含特定字符串的对象。到目前为止,我正在使用这个:

NSString username = "bob";
PFUser *user = [[PFUser alloc] init];

        for(PFUser *userItem in self.currentUser.friends) {

            if([user.username isEqualToString:username]) {

                user=userItem;
            }
        }

有没有更好的方法来做到这一点?这比使用 NSMutable 字典然后以这种方式拉出对象要慢得多吗?我的数组大小约为 100。谢谢

【问题讨论】:

  • NSPredicate 应该可以解决问题。但是如果你想优化,你应该在找到一个后退出循环。
  • 你想使用 NSMutableDictionary 而不是 NSMutableArray。 NSDictionary 是一种快速搜索的好方法,因为 Key 用于搜索。

标签: ios objective-c xcode nsmutablearray


【解决方案1】:

修改你的代码如下..使用 NSMutableArray..only

NSString username = "bob";
PFUser *user = [[PFUser alloc] init];

        for(PFUser *userItem in self.currentUser.friends) {
            //In your code it is written as user.username instead of userItem.username..check it..
            if([userItem.username isEqualToString:username]) {

                user=userItem;
                break;//exit from loop..Results in optimisation
            }
        }

使用 Predicate..of 对象数组

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", username];
NSArray *results = [self.currentUser.friends filteredArrayUsingPredicate:predicate];
PFUser *user=[[PFUser alloc]init];
user=[results objectAtIndex:0];//here is the object u need

希望对你有所帮助..!

【讨论】:

  • 谢谢。如果结果存在,那么永远只有一个。所有用户名都是唯一的。我真的需要将一个结果过滤到一个单独的数组中吗?这比我之前使用的循环快吗?
  • 使用谓词你可以做更多的优化然后循环..更多使用谓词结果将根据 NSArray 得到,所以在你的情况下,所有用户名都是唯一的,所以你可以直接使用结果数组的第一个对象..我修改了我的答案..检查..有用还是没用...!
【解决方案2】:

使用 NSPredicate,它应该看起来像这样:

  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.object.stringInsideObject LIKE %@", stringToCompare];

  NSArray *filteredArray = [[NSArray alloc] initWithArray:[yourArray filteredArrayUsingPredicate:predicate]];

【讨论】:

    【解决方案3】:

    试试这个,

    - (PFUser *)filterUserObjectWithName:(NSString *)userName
    {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.username == %@",userName];
        NSArray *results =  [self.currentUser.friends filteredArrayUsingPredicate:predicate];
        return results.count ? [results firstObject]: nil;
    }
    

    希望对你有帮助

    【讨论】:

    • 如果结果存在,则永远只有一个。所有用户名都是唯一的。我真的需要将一个结果过滤到一个单独的数组中吗?这比我之前使用的循环快吗?
    • 如果用户名是唯一的,那么您可以更改谓词以使用其他属性进行过滤。如果您使用 for 循环,则循环运行 100 次(例如,如果您在数组中有 100 个对象),即使找到所需的对象,您也应该按照Vidhyanand900 的建议使用break。我认为这是更好的方法,因为我们不是手动迭代所有元素,而且这两种方法(for loopNSPredicate)无论如何都必须遍历所有元素。
    【解决方案4】:
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF. username contains[c] %@", stringToCompare];
    
    NSArray *filteredArray = [[NSArray alloc] initWithArray:[yourArray filteredArrayUsingPredicate:predicate]];
    

    希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-10-09
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      相关资源
      最近更新 更多