gpt4 book ai didi

iphone - 规范化或规范化核心数据的字符串?

转载 作者:可可西里 更新时间:2023-11-01 06:15:49 25 4
gpt4 key购买 nike

我看过一些关于 Core Data 的 WWDC 视频,我正计划维护一个规范化的文本属性。假设我有以下数据:

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

当我查询我的模型时,我想按“normalizedString”对其进行排序,以便它忽略大小写和 Ø(或其他字符)。我还希望能够运行类似“以‘o’开头”的查询,并让它返回上面的 3 个词。

我试图避免做类似的事情:

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

用于查询模型。我还尝试使用“originalString”进行排序。

我尝试了两种不同的方法但都没有成功,我的规范化字符串仍然保存为原始字符串(我覆盖了我创建的类别中的 setter ):

  1. 调用 decomposedStringWithCanonicalMapping:

    // ...
    [normalizedString decomposedStringWithCanonicalMapping];
    // ...
  2. 已关注 this example :

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

关于如何实现我的目标有什么想法吗?

编辑:这是我重写的 setter,我知道它被称为:

- (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"];
}

最佳答案

您应该覆盖“主要”属性(例如 firstNamelastName)的 setter,而不是“派生”属性的 setter。

还要注意 decomposedStringWithCanonicalMapping 返回一个新字符串,它不会修改接收器。

代码大致如下所示(未经编译器检查):

- (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;
}

关于iphone - 规范化或规范化核心数据的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17418443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com