Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSPredicate-使用两个条件进行评估_Iphone_Objective C_Regex_Nspredicate - Fatal编程技术网

Iphone NSPredicate-使用两个条件进行评估

Iphone NSPredicate-使用两个条件进行评估,iphone,objective-c,regex,nspredicate,Iphone,Objective C,Regex,Nspredicate,我想知道如何使用NSPredicate计算满足两个条件的NSString。例如,如何检查至少应该有一个大写字母和一个数字的字符串。您可以对两个或多个条件使用ANDing NSManagedObjectContext *context = [appDelegate managedObjectContext]; NSEntityDescription *entityDescription = [NSEntityDescription entityForName:entityName inMana

我想知道如何使用NSPredicate计算满足两个条件的NSString。例如,如何检查至少应该有一个大写字母和一个数字的字符串。

您可以对两个或多个条件使用ANDing

 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来完成它。谢谢你的帮助。。