본문 바로가기
  • SDXL 1.0 + 한복 LoRA
  • SDXL 1.0 + 한복 LoRA
Development/iPhone

[옛 글] [CoreData] 조금은 황당하고 조금은 당황스러운 팁...

by 마즈다 2013. 7. 19.
반응형

최초 작성일 : 2012/04/13 10:12 


일반적인 관계형 DBMS의 쿼리에서 where절의 역할을 하는 것이

코어 데이터의 NSPredicate 객체이다.

대체로 문법이 비슷하긴 하지만 아무래도 복잡한 쿼리를 구성하기에는 좀 답답한
감이 있다.

더구나 다음과 같은 케이스는 불필요한 코딩만 늘리는 것 같은데 이렇게 해야 한단다...-.-

NSPredicate를 이용한 like문은 대략 다음과 같다.
우선 일반적인 쿼리에서 like문에 사용하는 %는 CoreData는 *로 사용한다.

NSPredicate *predicate;

NSString *searchStr = [NSString stringWithFormat:@"*%@*", toMail];

predicate = [NSPredicate predicateWithFormat:@"%K LIKE[cd] %@ and %K LIKE[cd] %@",
attributeName, self.mailAddress, attributeName2, searchStr];

보시는 바와같이 그냥
predicate = [NSPredicate predicateWithFormat:@"%K LIKE[cd] %@ and %K LIKE[cd] *%@*",
attributeName, self.mailAddress, attributeName2, toMail];

이렇게 하면 될 것 같은데 위의 문장으로 해야만 검색이 정상적으로 이루어진다...-.-

*참고 :
LIKE[cd]에서 cd의 의미는 다음과 같다.

'cd' means case-insensitive and diacritic-insensitive.

즉, 대소문자 구분 안하고. 발음기호 구분 안한다는 의미이다.

반응형