programing

NULL 및 빈 문자열을 테스트하는 NSPredicate

nasanasas 2020. 10. 25. 12:24
반응형

NULL 및 빈 문자열을 테스트하는 NSPredicate


나는 NSArraynull이거나 오히려 ''(빈 문자열)이있는 모든 문자열을 필터링해야합니다. 어떻게하나요? 나는 시도했다 :

NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"(name!=nil)"]; 

그러나 그것은 작동하지 않는 것 같습니다. 아니면 그럴 수도 있지만 다른 종류의 null이 있습니다.


Core Data를 사용하지 않는 경우 다음을 수행 할 수 있습니다.

NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"name.length > 0"];

문자열이 비어 있으면 실패합니다 (때문에 0 == 0). 경우 마찬가지로 name이며 nil, 또한 실패 때문에 것입니다 [nil length] == 0.


나는 이것이 효과가 있다고 생각한다.

NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"name!=nil AND name!=''"]; 

 NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"name!=NULL"];

이 술어는 나를 위해 일했습니다.

[NSPredicate predicateWithFormat:@"(%K== nil) OR %K.length == 0", @"imageUrl", @"imageUrl"]

참고 URL : https://stackoverflow.com/questions/7369390/nspredicate-to-test-for-null-and-blank-strings

반응형