iOS nspredicate (regular Expressions and Uibarcontroller): predicates

Source: Internet
Author: User

    • Instantiating the header file of an object
    • @property (nonatomic, strong) NSString *name;
      @property (nonatomic, assign) NSString *sex;
      @property (nonatomic, assign) Nsinteger tel;
      @property (nonatomic, assign) Nsinteger QQ;

      -(Member *) Initwithdic: (Nsdictionary *) dic;
      + (Member *) Memberwithdic: (Nsdictionary *) dic;
    • Implementation of the constructor function

-(Member *) Initwithdic: (Nsdictionary *) dic
{
if (self = [super init])
{
_name = dic[@ "name"];
_sex = dic[@ "Sex"];
_tel = [dic[@ "tel"] integervalue];
_QQ = [dic[@ "QQ"] integervalue];
}
return self;
}

+ (Member *) Memberwithdic: (Nsdictionary *) dic
{
return [[Self alloc] initwithdic:dic];
}

-(NSString *) description
{
return [NSString stringwithformat:@ "name =%@, sex =%@, QQ =%lu, tel =%lu", _name, _sex, _QQ, _tel];
}

    • Get data Source: Instantiate a plist file


Cannot get path using NSBundle
Nsarray *arrdata = [Nsarray arraywithcontentsoffile:@ "/users/mu/desktop/predicates and regular expressions/predicates and regular Expressions/iosclass (2). plist"];

Collection stores all instantiated objects: plist instantiation of the
(modeled): The object's properties are judged, the set of operations and the set
Nsmutablearray *arrmember = [Nsmutablearray array];

For (Nsarray *arrgroup in Arrdata)
{
For (Nsdictionary *dicmember in Arrgroup)
{
[Arrmember Addobject:[[member alloc] initwithdic:dicmember];
}
}
Strings need to use single quotes, statement case insensitive but attribute is variable case sensitive

1. Relational operations
-Determine if (contains the same as the judging object)
Nspredicate *predicate1 = [nspredicate predicatewithformat:@ "name = = ' Yanjiang ' and QQ = 287143225"];
For (Member *mem in [Arrmember filteredarrayusingpredicate:predicate1])
{
NSLog (@ "1.%@", mem);
}

2. Relational operations
<= equivalent =<;! = and <>; = = and >=;! and not; Equal = and ==,and and &&; OR and | |
Nspredicate *predicate2 = [nspredicate predicatewithformat:@ "not name.length <= 3"];
For (Member *mem in [Arrmember Filteredarrayusingpredicate:predicate2])
{
NSLog (@ "2.%@", mem);
}

3.CONTAINS
-[C] ignore case
-[d] ignore accent marks
-[CD] is case-insensitive and does not distinguish between pronounced symbols
Nspredicate *predicate3 = [nspredicate predicatewithformat:@ "Name contains[cd]%@", @ "Yan"];
For (Member *mem in [Arrmember Filteredarrayusingpredicate:predicate3])
{
NSLog (@ "3.%@", mem);
}

4.BEGINSWITH and EndsWith: Judging with the declaration beginning or end
Nspredicate *predicate4 = [nspredicate predicatewithformat:@ "name Beginswith%@", @ "Yan"];
For (Member *mem in [Arrmember filteredarrayusingpredicate:predicate4])
{
NSLog (@ "4.%@", mem);
}
Nspredicate *predicate5 = [nspredicate predicatewithformat:@ "name ENDSWITH%@", @ "Yan"];
For (Member *mem in [Arrmember Filteredarrayusingpredicate:predicate5])
{
NSLog (@ "4.%@", mem);
}

5. A wildcard like
-* represents one or more or empty
-? Represents a character
Nspredicate *predicate6 = [nspredicate predicatewithformat:@ "name like%@", @ "* jiang"];
For (Member *mem in [Arrmember Filteredarrayusingpredicate:predicate6])
{
NSLog (@ "5.%@", mem);
}


6. Relational operations
-in {}: Determines whether an object exists within the specified collection (and multiple equivalent using an or connection)
Nspredicate *predicate7 = [Nspredicate predicatewithformat:@ "tel in {18798812696, 18798815221}"];
For (Member *mem in [Arrmember Filteredarrayusingpredicate:predicate7])
{
NSLog (@ "6.%@", mem);
}
Nspredicate *predicate8 = [nspredicate predicatewithformat:@ "name in%@", @[@ "Yanjiang", @ "Liu Mu"]];
For (Member *mem in [Arrmember Filteredarrayusingpredicate:predicate8])
{
NSLog (@ "6.%@", mem);
}
7. Relational operations
-Between {}: Specify the range to determine if the data source is

Nspredicate *predicate9 = [Nspredicate predicatewithformat:@ "Tel between {18798812696, 18798815221}"];
For (Member *mem in [Arrmember Filteredarrayusingpredicate:predicate9])
{
NSLog (@ "7.%@", mem);
}

8. Relational operations
-Not the most common use is to exclude data from an array from another array
-add more than one or two points to the new array compared to the loop comparison.
Nsarray *arrayfilter = @[@ "ABC1", @ "ABC2"];
Nsarray *arraycontent = @[@ "A1", @ "ABC1", @ "ABC4", @ "ABC2"];
Nspredicate *thepredicate = [nspredicate predicatewithformat:@ ' not ' (self in%@) ", Arrayfilter];
NSLog (@ "8.%@", [arraycontent filteredarrayusingpredicate:thepredicate]);

9.block
-Parameter evaluatedobject represents an array member
-Block must return yes or No to indicate match or mismatch, respectively
Nspredicate *predicate10 = [nspredicate predicatewithblock:^bool (id _nonnull evaluatedobject, NSDictionary< NSString *,id> * _nullable bindings)
{
Member *mem = (Member *) Evaluatedobject;
if (mem.name.length>4)
{
return YES;
}else
{
return false;
}
}];

For (Member *mem in [Arrmember Filteredarrayusingpredicate:predicate10])
{
NSLog (@ "9.%@", mem);
}

10. Multiple screening
-If you need to match a filter of several attributes, it's a little cumbersome to concatenate with and OR OR.
-Nscompoundpredicate can combine multiple Nspredicate objects
-The combination can be and or OR and not: Call the class method to create the required parameter is a collection of Nspredicate objects

11. Attention Points
-%k K must be uppercase: Use%@ =%@ will appear mismatch
Nspredicate *predicate11 = [Nspredicate predicatewithformat:@ "%@ =%@", @ "name", @ "Yanjiang"];
For (Member *mem in [Arrmember Filteredarrayusingpredicate:predicate11])
{
NSLog (@ "10.%@", mem);
}
Nspredicate *predicate12 = [Nspredicate predicatewithformat:@ "%K =%@", @ "name", @ "Yanjiang"];
For (Member *mem in [Arrmember Filteredarrayusingpredicate:predicate12])
{
NSLog (@ "10.%@", mem);
}


12. Regular Expressions
Nspredicate *pri = [Nspredicate predicatewithformat:@ "Self MATChES%@", @ "^[1-9] (\\d{5,11})"];
NSLog (@ "12. Determine if the QQ number:%s", ([pri evaluatewithobject:@ "287143225"]==yes)? " YES ":" NO ");

iOS nspredicate (regular Expressions and uibarcontroller): verbs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.