खोज…


वाक्य - विन्यास

  • प्रारूप स्ट्रिंग स्ट्रिंग पदार्थों को समर्पित करें
    • सी प्रारूप स्ट्रिंग विनिर्देशक:% d,% s,% f, आदि
    • ऑब्जेक्ट प्रतिस्थापन:% @
    • कीपथ प्रतिस्थापन:% के
  • तुलना ऑपरेटरों को समर्पित करें
    • =, ==: बाएं हाथ की अभिव्यक्ति दाएं हाथ की अभिव्यक्ति के बराबर है
    • > =, =>: बाएं हाथ की अभिव्यक्ति दाएं हाथ की अभिव्यक्ति की तुलना में अधिक या बराबर है
    • <=, = <: बाएं हाथ की अभिव्यक्ति दाहिने हाथ की अभिव्यक्ति से कम या बराबर है
    • >: बाएं हाथ की अभिव्यक्ति दाएं हाथ की अभिव्यक्ति से अधिक है
    • <: बाएं हाथ की अभिव्यक्ति दाहिने हाथ की अभिव्यक्ति से कम है
    • ! =, <>: बाएं हाथ की अभिव्यक्ति दाहिने हाथ की अभिव्यक्ति के बराबर नहीं है
    • शर्त: बाएं हाथ की अभिव्यक्ति दाएं हाथ की अभिव्यक्ति के किसी भी मान के बराबर या बराबर होती है, जो निचले और ऊपरी सीमा को निर्दिष्ट करती है - पूर्व: BETWEEN {0, 5}
  • कंपाउंड ऑपरेटरों को समर्पित करें
    • और, &&: तार्किक और
    • या, ||: तार्किक या
    • नहीं ;:: तार्किक नहीं
  • स्ट्रिंग की तुलना ऑपरेटरों से करें
    • BEGINSWITH: बाएं हाथ की अभिव्यक्ति दाएँ हाथ की अभिव्यक्ति से शुरू होती है
    • ENDSWITH: बाएं हाथ की अभिव्यक्ति दाहिने हाथ की अभिव्यक्ति के साथ समाप्त होती है
    • निष्कर्ष: बाएं हाथ की अभिव्यक्ति में दाहिने हाथ की अभिव्यक्ति होती है
    • LIKE: बाएँ हाथ की अभिव्यक्ति, वाइल्डकार्ड प्रतिस्थापन के साथ दाहिने हाथ की अभिव्यक्ति के बराबर है
      • *: शून्य या अधिक वर्णों का मिलान करें
      • ?: एक चरित्र का मिलान करें

विधेयवाद का उपयोग करके एक NSPredicate बनाना

उद्देश्य सी

NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id item, 
                                                               NSDictionary *bindings) {
    return [item isKindOfClass:[UILabel class]];
}];

तीव्र

let predicate = NSPredicate { (item, bindings) -> Bool in
    return item.isKindOfClass(UILabel.self)
}

इस उदाहरण में, विधेय उन वस्तुओं से मेल खाएगा जो कक्षा UILabel

एक NSPredicate बनाना predicateWithFormat का उपयोग करना

उद्देश्य सी

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"self[SIZE] = %d", 5)];

तीव्र

let predicate = NSPredicate(format: "self[SIZE] >= %d", 5)

इस उदाहरण में, विधेय उन वस्तुओं से मेल खाएगा जो कम से कम 5 की लंबाई के साथ सरणियां हैं।

प्रतिस्थापन चर के साथ एक NSPredicate बनाना

एक NSPredicate मूल्यों को उड़ने के लिए बाध्य करने के लिए प्रतिस्थापन चर का उपयोग कर सकता है।

उद्देश्य सी

NSPredicate *template = [NSPredicate predicateWithFormat: @"self BEGINSWITH $letter"];
NSDictionary *variables = @{ @"letter": @"r" };
NSPredicate *beginsWithR = [template predicateWithSubstitutionVariables: variables];

तीव्र

let template = NSPredicate(format: "self BEGINSWITH $letter")
let variables = ["letter": "r"]
let beginsWithR = template.predicateWithSubstitutionVariables(variables)

टेम्पलेट विधेय को predicateWithSubstitutionVariables द्वारा संशोधित नहीं किया गया predicateWithSubstitutionVariables । इसके बजाय, एक प्रति बनाई जाती है, और उस प्रति को प्रतिस्थापन चर प्राप्त होता है।

एक सरणी को फ़िल्टर करने के लिए NSPredicate का उपयोग करना

उद्देश्य सी

NSArray *heroes = @[@"tracer", @"bastion", @"reaper", @"junkrat", @"roadhog"];

NSPredicate *template = [NSPredicate predicateWithFormat:@"self BEGINSWITH $letter"];

NSDictionary *beginsWithRVariables = @{ @"letter": @"r"};
NSPredicate *beginsWithR = [template predicateWithSubstitutionVariables: beginsWithRVariables];

NSArray *beginsWithRHeroes = [heroes filteredArrayUsingPredicate: beginsWithR];
// ["reaper", "roadhog"]

NSDictionary *beginsWithTVariables = @{ @"letter": @"t"};
NSPredicate *beginsWithT = [template predicateWithSubstitutionVarables: beginsWithTVariables];

NSArray *beginsWithTHeroes = [heroes filteredArrayUsingPredicate: beginsWithT];
// ["tracer"]

तीव्र

let heroes = ["tracer", "bastion", "reaper", "junkrat", "roadhog"]

let template = NSPredicate(format: "self BEGINSWITH $letter")

let beginsWithRVariables = ["letter": "r"]
let beginsWithR = template.predicateWithSubstitutionVariables(beginsWithRVariables)

let beginsWithRHeroes = heroes.filter { beginsWithR.evaluateWithObject($0) }
// ["reaper", "roadhog"]

let beginsWithTVariables = ["letter": "t"]
let beginsWithT = template.predicateWithSubstitutionVariables(beginsWithTVariables)

let beginsWithTHeroes = heroes.filter { beginsWithT.evaluateWithObject($0) }
// ["tracer"]

NSPredicate का उपयोग करके प्रपत्र सत्यापन

NSString *emailRegex = @"[A-Z0-9a-z]([A-Z0-9a-z._-]{0,64})+[A-Z0-9a-z]+@[A-Z0-9a-z]+([A-Za-z0-9.-]{0,64})+([A-Z0-9a-z])+\\.[A-Za-z]{2,4}";    NSString *firstNameRegex = @"[0-9A-Za-z\"'-]{2,32}$";
NSString *firstNameRegex = @"[ 0-9A-Za-z]{2,32}$";
NSString *lastNameRegex = @"[0-9A-Za-z\"'-]{2,32}$";
NSString *mobileNumberRegEx = @"^[0-9]{10}$";
NSString *zipcodeRegEx = @"^[0-9]{5}$";
NSString *SSNRegEx = @"^\\d{3}-?\\d{2}-?\\d{4}$";
NSString *addressRegEx = @"^[ A-Za-z0-9]{2,32}$";
NSString *cityRegEx = @"^[ A-Za-z0-9]{2,25}$";
NSString *PINRegEx = @"^[0-9]{4}$";
NSString *driversLiscRegEx = @"^[0-9a-zA-Z]{5,20}$";

-(BOOL)validateEmail {
    //Email address field should give an error when the email address begins with ".","-","_" .
    NSPredicate *emailPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];   
    return ([emailPredicate evaluateWithObject:self.text] && self.text.length <= 64 && ([self.text rangeOfString:@".."].location == NSNotFound));
}

- (BOOL)validateFirstName {
    NSPredicate *firstNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", firstNameRegex];
    return [firstNamePredicate evaluateWithObject:self.text];
}

- (BOOL)validateLastName {
    NSPredicate *lastNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", lastNameRegex];
    return [lastNamePredicate evaluateWithObject:self.text];
}

- (BOOL)validateAlphaNumericMin2Max32 {
    NSPredicate *firstNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", firstNameRegex];
    return [firstNamePredicate evaluateWithObject:self.text];
}

- (BOOL)validateMobileNumber {
    NSString *strippedMobileNumber =  [[[[self.text stringByReplacingOccurrencesOfString:@"(" withString:@""]
                                        stringByReplacingOccurrencesOfString:@")" withString:@""]
                                        stringByReplacingOccurrencesOfString:@"-" withString:@""]
                                        stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    NSPredicate *mobileNumberPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobileNumberRegEx];
    
    return [mobileNumberPredicate evaluateWithObject:strippedMobileNumber];
}

- (BOOL)validateZipcode {
    NSPredicate *zipcodePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", zipcodeRegEx];
    
    return [zipcodePredicate evaluateWithObject:self.text];
}

- (BOOL)validateSSN {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", SSNRegEx];

return [predicate evaluateWithObject:self.text];
}

- (BOOL)validateAddress {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", addressRegEx];
    
    return [predicate evaluateWithObject:self.text];
}

- (BOOL)validateCity {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", cityRegEx];
    return [predicate evaluateWithObject:self.text];
}

- (BOOL)validatePIN {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PINRegEx];    
    return [predicate evaluateWithObject:self.text];
}
   - (BOOL)validateDriversLiscNumber {
    if([self.text length] > 20) {
        return NO;
    }
    NSPredicate *driversLiscPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", driversLiscRegEx];
    
    return [driversLiscPredicate evaluateWithObject:self.text];
}

NSPredicate `AND`,` OR` और `NOT` की शर्त के साथ

सशर्त विधेयक NSCompoundPredicate वर्ग का उपयोग करके क्लीनर और सुरक्षित होगा जो दिए गए विधेय के लिए बुनियादी बूलियन ऑपरेटरों को प्रदान करता है।

उद्देश्य सी

और - हालत

  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"samplePredicate"];
  NSPredicate *anotherPredicate = [NSPredicate predicateWithFormat:@"anotherPredicate"];
  NSPredicate *combinedPredicate = [NSCompoundPredicate andPredicateWithSubpredicates: @[predicate,anotherPredicate]];

या - हालत

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"samplePredicate"];
 NSPredicate *anotherPredicate = [NSPredicate predicateWithFormat:@"anotherPredicate"];
 NSPredicate *combinedPredicate = [NSCompoundPredicate orPredicateWithSubpredicates: @[predicate,anotherPredicate]];

नहीं - हालत

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"samplePredicate"];
 NSPredicate *anotherPredicate = [NSPredicate predicateWithFormat:@"anotherPredicate"];
 NSPredicate *combinedPredicate = [NSCompoundPredicate notPredicateWithSubpredicate: @[predicate,anotherPredicate]];


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow