I've watched some of the WWDC videos on Core Data and I'm planning on maintaining a canonicalized text property. Let's say I have the following data:
originalString normalizedString (+lowercase?)
Ønsker onsker
onsker onsker
Onsker onsker
When I query my model, I want to sort it by 'normalizedString' so that it ignores the case and the Ø (or other characters). I also want to be able to run a query like "starts with 'o'" and have it return the 3 words above.
I was trying to avoid to do something like:
[NSPredicate predicateWithFormat:@"(originalString like[cd] %@)"...
for querying the model. I was also trying to use the 'originalString' for my sorting.
I've tried two different approaches with no success, my normalized string is still saved as the originalString (I override the setter in a category I created):
Calling decomposedStringWithCanonicalMapping:
// ... [normalizedString decomposedStringWithCanonicalMapping]; // ...
Followed this example:
// ... CFStringNormalize((CFMutableStringRef)normalizedString, kCFStringNormalizationFormD); CFStringFold((CFMutableStringRef)normalizedString, kCFCompareCaseInsensitive | kCFCompareDiacriticInsensitive | kCFCompareWidthInsensitive, NULL);
Any ideas on how I can accomplish my goal?
Edit: Here's my overridden setter, which I know it gets called:
- (void) setNormalizedName:(NSString *)newNormalizedName
{
NSMutableString *normalizedString;
if (![self.lastName length] == 0) {
normalizedString = [NSMutableString stringWithString:self.lastName];
} else {
normalizedString = [NSMutableString stringWithString:self.firstName];
}
// CFStringNormalize((CFMutableStringRef)normalizedString, kCFStringNormalizationFormD);
// CFStringFold((CFMutableStringRef)normalizedString, kCFCompareCaseInsensitive | kCFCompareDiacriticInsensitive | kCFCompareWidthInsensitive, NULL);
[normalizedString decomposedStringWithCanonicalMapping];
[self willChangeValueForKey:@"normalizedName"];
[self setPrimitiveValue:normalizedString forKey:@"normalizedName"];
[self didChangeValueForKey:@"normalizedName"];
}