Iphone NSPredicate-使用两个条件进行评估
我想知道如何使用NSPredicate计算满足两个条件的NSString。例如,如何检查至少应该有一个大写字母和一个数字的字符串。您可以对两个或多个条件使用ANDingIphone NSPredicate-使用两个条件进行评估,iphone,objective-c,regex,nspredicate,Iphone,Objective C,Regex,Nspredicate,我想知道如何使用NSPredicate计算满足两个条件的NSString。例如,如何检查至少应该有一个大写字母和一个数字的字符串。您可以对两个或多个条件使用ANDing NSManagedObjectContext *context = [appDelegate managedObjectContext]; NSEntityDescription *entityDescription = [NSEntityDescription entityForName:entityName inMana
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entityDescription];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"fbUID = %@ AND birthDate = %d AND birthMonth = %d AND greetingYear = %d", uID, birthDate, birthMonth, greetingYear];
//NSLog(@"%@ :",predicate);
[fetchRequest setPredicate:predicate];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
您可以在NSPredicate上查看更多信息。一种方法是:
NSCharacterSet *upperCaseCharacters = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLKMNOPQRSTUVWXYZ"];
NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
return ([evaluatedObject rangeOfCharacterFromSet:upperCaseCharacters].location != NSNotFound && [evaluatedObject rangeOfCharacterFromSet:numbers].location != NSNotFound);
}];
NSString *stringToEvaluate = @"aasad5D";
BOOL result = [predicate evaluateWithObject:stringToEvaluate];
另一个解决方案是复合谓词,请参见这个问题,例如如何使用复合谓词。我自己找到了一个解决方案。我使用带谓词的正则表达式来解决这个问题
NSPredicate *pwdCheck = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"(?=.*?[A-Z])(?=.*?[0-9]).*"];
bool isValid = [pwdCheck evaluateWithObject:@"99a99A3w"];
谢谢你的帮助嗨。。你的答案似乎是正确的。但我可以在几行代码中使用reg-ex来完成它。谢谢你的帮助嗨。。你的答案似乎是正确的。但我可以在几行代码中使用reg-ex来完成它。谢谢你的帮助。。