Application of iOS predicate

Keywords: Android Mobile SQL Database

The predicate in Cocoa provides a general query method to process data, The filtering form of data can be obtained and specified. In the actual development of Cocoa, NSPredicate and its parent classes NSComparisonPredicate and nscompoundpredict can be used. Its style is similar to the mixture of SQL query language and regular expression, providing a expressive, natural language interface to define a set of logical conditions to be searched. Generally speaking, it is easy to understand the method of database after a little operation, and the method used is also very simple. The following code is the implementation method:

BIDValidateMgr.h Code:

//
//  BIDValidateMgr.h
//  TongHuiShop
//
//  Created by eJiupi on 14-10-29.
//  Copyright (c) 2014 year xujinzhong. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BIDValidateMgr : NSObject

//Mobile number verification
+ (BOOL)validateMobile:(NSString *)mobile;

//mailbox
+ (BOOL)validateEmail:(NSString *)email;

//ID number
+ (BOOL)validateIdentityCard: (NSString *)identityCard;

@end

BIDValidateMgr.m implementation code;

//
//  BIDValidateMgr.m
//  TongHuiShop
//
//  Created by eJiupi on 14-10-29.
//  Copyright (c) 2014 year xujinzhong. All rights reserved.
//

#import "BIDValidateMgr.h"

@implementation BIDValidateMgr

//Mobile number verification
+ (BOOL)validateMobile:(NSString *)mobile
{
    //Phone numbers start with 13, 15, 18, eight \d Numeric character
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}

//mailbox
+ (BOOL)validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

//ID number
+ (BOOL)validateIdentityCard: (NSString *)identityCard
{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}

@end

Posted by Anyaer on Wed, 01 Apr 2020 22:49:36 -0700