Note that there are some explanatory texts on larger screens.

plurals
  1. PONormalize or Canonicalize string for Core Data?
    primarykey
    data
    text
    <p>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:</p> <pre><code>originalString normalizedString (+lowercase?) Ønsker onsker onsker onsker Onsker onsker </code></pre> <p>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.</p> <p>I was trying to avoid to do something like:</p> <pre><code>[NSPredicate predicateWithFormat:@"(originalString like[cd] %@)"... </code></pre> <p>for querying the model. I was also trying to use the 'originalString' for my sorting.</p> <p>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):</p> <ol> <li><p>Calling decomposedStringWithCanonicalMapping:</p> <pre><code>// ... [normalizedString decomposedStringWithCanonicalMapping]; // ... </code></pre></li> <li><p>Followed <a href="https://developer.apple.com/library/mac/#samplecode/DerivedProperty/Introduction/Intro.html" rel="nofollow">this example</a>: </p> <pre><code>// ... CFStringNormalize((CFMutableStringRef)normalizedString, kCFStringNormalizationFormD); CFStringFold((CFMutableStringRef)normalizedString, kCFCompareCaseInsensitive | kCFCompareDiacriticInsensitive | kCFCompareWidthInsensitive, NULL); </code></pre></li> </ol> <p>Any ideas on how I can accomplish my goal?</p> <p>Edit: Here's my overridden setter, which I know it gets called:</p> <pre><code>- (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"]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload