Normalize or canonize a row for master data?

I watched some WWDC videos on the basic data, and I plan to preserve the canonicalized text property. Let's say I have the following data:

originalString normalizedString (+lowercase?) Ønsker onsker onsker onsker Onsker onsker 

When I request my model, I want to sort it by "normalizedString" so that it ignores case and character (or other characters). I also want to be able to run a query like "starts with" o "and return 3 words to it.

I tried to avoid to do something like:

 [NSPredicate predicateWithFormat:@"(originalString like[cd] %@)"... 

to request a model. I also tried using "originalString" for my sorting.

I tried two different approaches without success, my normalized string is still saved as originalString (I redefine the setter in the category I created):

  • The call to decposedStringWithCanonicalMapping:

     // ... [normalizedString decomposedStringWithCanonicalMapping]; // ... 
  • Further this example :

     // ... CFStringNormalize((CFMutableStringRef)normalizedString, kCFStringNormalizationFormD); CFStringFold((CFMutableStringRef)normalizedString, kCFCompareCaseInsensitive | kCFCompareDiacriticInsensitive | kCFCompareWidthInsensitive, NULL); 

Any ideas on how I can achieve my goal?

Edit: Here, my overridden setter that I know is 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"]; } 
+4
source share
1 answer

You should override the setters for the "primary" properties (for example, firstName , lastName ), and not the installer for the derived property.

Also note that decomposedStringWithCanonicalMapping returns a new line, this does not change the receiver.

The code may look something like this (not checked by the compiler):

 - (void) setFirstName:(NSString *)firstName { [self willChangeValueForKey:@"firstName"]; [self setPrimitiveValue:firstName forKey:@"firstName"]; [self didChangeValueForKey:@"firstName"]; [self updateNormalizedName]; } - (void) setLastName:(NSString *)lastName { [self willChangeValueForKey:@"lastName"]; [self setPrimitiveValue:lastName forKey:@"lastName"]; [self didChangeValueForKey:@"lastName"]; [self updateNormalizedName]; } - (void) updateNormalizedName { NSString *normalizedString; if ([self.lastName length] > 0) { normalizedString = [self.lastName decomposedStringWithCanonicalMapping]; } else { normalizedString = [self.firstName decomposedStringWithCanonicalMapping]; } self.normalizedString = normalizedString; } 
0
source

Source: https://habr.com/ru/post/1489208/


All Articles