cool hit counter Objective-C array filtering_Intefrankly

Objective-C array filtering


Think projects are likely to encounter filtering of a certain array according to certain conditions. It may not be efficient to traverse the array directly and take the objects that match the conditions and put them into a new array.

Here's a simpler way to do it

brief introduction

NSPredicate class is mainly used to specify the conditions of the filter, the object can accurately describe the required conditions, each object through the predicate to filter, determine whether the conditions match. A predicate is a function that represents the calculation of a true or false value in a computer. The principle and usage are similar to where in a SQL query, which is equivalent to databases The filtered take. It is mainly used to sort out eligible objects or data models from a collection, and can also be used for regular matching of strings. generic, NSPredicate The conditions for the filtering filter can be, Logical operation symbols(> , < , =), range operator(IN,BETWEEN), The inclusion of the character/ match/ Fuzzy Search(BEGINSWITH,ENDSWITH,CONTAINS,LIKE), regular expression(MATCHES) etc..

NSPredicate Usage 1.Creating an NSPredicate NSPredicate *predicate = [NSPredicate predicateWithFormat:@" Filtering conditions"]; 2.Determines if the specified object meets the filter conditions created by NSPredicate [predicate evaluateWithObject:model]; 3.Filter the array by the created predicate and return the data that matches the conditions NSArray *resultArr = [array filteredArrayUsingPredicate:predicate];

For a concrete implementation, first we define a Peron class

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (copy, nonatomic) NSString *name;
@property (assign, nonatomic) NSInteger age;

- (instancetype)initWithName:(NSString *)name age:(NSInteger)age;

-(void)printPerson ;
#import "Person.h"

@interface Person()

@end

@implementation Person

- (instancetype)initWithName:(NSString *)name age:(NSInteger)age {
    self = [super init];
    if (self) {
        self.name = name;
        self.age = age;
    }
    return self;
}

-(void)printPerson {
    NSLog(@"Name is %@ ageis %zd ",_name, _age);
}

In defining a Category of an NSMutableArray NSMutableArray+Extended

#import <Foundation/Foundation.h>

@interface NSMutableArray (Extended)

/**
  Arrays filtering objects with the Int type property

  @param condition filter condition
  @param value The value corresponding to the attribute
  @return filtered array
 */
-(NSMutableArray*)getArrayWithInt:(NSString*)condition value:(NSInteger)value;


/**
  Arrays filter objects whose attributes are of type NSString

  @param key The attribute field corresponding to the object
  @param value The value corresponding to the object
  @return filtered array
 */
-(NSMutableArray*)getArrayWithString:(NSString*)key value:(NSString*)value;

@end
#import "NSMutableArray+Extended.h"

@implementation NSMutableArray (Extended)

-(NSMutableArray*)getArrayWithInt:(NSString*)condition value:(NSInteger)value
{
    NSPredicate *inputPredicate=[NSPredicate predicateWithFormat:condition,value];
    NSMutableArray *newMutableArray = [NSMutableArray arrayWithArray:[self filteredArrayUsingPredicate:inputPredicate]];
    return newMutableArray;
}

-(NSMutableArray*)getArrayWithString:(NSString*)key value:(NSString*)value
{
    NSPredicate *inputPredicate=[NSPredicate predicateWithFormat:@"%K like[cd] %@",key, [NSString stringWithFormat:@"*%@*",value]];
    NSMutableArray *newMutableArray = [NSMutableArray arrayWithArray:[self filteredArrayUsingPredicate:inputPredicate]];
    return newMutableArray;
}

@end

The following call

Person *p1 = [[Person alloc]initWithName:@"aa" age:18];
Person *p2 = [[Person alloc]initWithName:@"bb" age:18];
Person *p3 = [[Person alloc]initWithName:@"cc" age:18];
Person *p4 = [[Person alloc]initWithName:@"dd" age:22];
Person *p5 = [[Person alloc]initWithName:@"ee" age:15];
    
NSMutableArray *personArr = [NSMutableArray arrayWithObjects:p1,p2,p3,p4,p5, nil];
//  Fetch the array ofage = 18 object of
[[personArr getArrayWithInt:@"age=%d" value:18] makeObjectsPerformSelector:@selector(printPerson)];
//  Fetch the array ofname = @"aa" object of
[[personArr getArrayWithString:@"name" value:@"aa"] makeObjectsPerformSelector:@selector(printPerson)];

Recommended>>
1、General knowledge of webview in app above
2、21 MustHave Command Line Tools for Data Scientists
3、Blockchain zooms in again to help produce traceability technology knots up
4、Nerdy robots play with kids while they learn
5、Heart of the Machine Online Share 3 Deep Neural Networks Stochastic Ternary Gradient Descent and Structured Sparse

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号