NSPredicate Character and/or Letter Substitutions

血红的双手。 提交于 2019-12-24 07:09:42

问题


I've seen some, somewhat, similar questions about filtering out special characters in a predicate - but nothing suitable for my needs. I'm not really sure if what I want to do is even possible but I'll put it to the community instead:

I'm working with strings of pronunciations for a dictionary that contain a lot of IPA characters for instance: ȵ ɕ ŋ Ẓ

What I would like to do is set up some substitutions for these characters, something like: n=ȵ x=ɕ ng=ŋ r=Ẓ

in a search predicate so that a search for n* would result with anything beginning in ȵ, etc, etc...

Even other IPA 'letters' like "v" would be best resembled by a "w" if there is a simple way to switch in SEARCH letters for other STRING letters that would be great...


回答1:


It seems like this should be done outside the predicate. Create a dictionary where the keys are the strings to be replaced and the values are the replacements. Then, have a loop which iterates over the dictionary and performs each replacement on the string that you will be searching. The resulting string after replacement is the string that you use in the predicate.




回答2:


got the answer from here: stackoverflow.com/questions/21852502/nspredicate-ignore-numbers-in-string-pinyin an other question to which I asked...

this is the code [incase this is helpful for anyone else later] I used for string "pro" in class Words

- (NSString *)searchableStringValueTwo {
    NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:@"ȵ"];
    NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:@"n"];
    return searchString;
}

- (NSString *)searchableStringValueThree {
    NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:@"ɕ"];
    NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:@"x"];
    return searchString;
}

- (NSString *)searchableStringValueFour {
    NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:@"ŋ"];
    NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:@"ng"];
    return searchString;
}

- (NSString *)searchableStringValueFive {
    NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:@"Ẓ"];
    NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:@"r"];
    return searchString;
}

- (NSString *)searchableStringValueSix {
    NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:@"v"];
    NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:@"w"];
    return searchString;
}


来源:https://stackoverflow.com/questions/21844888/nspredicate-character-and-or-letter-substitutions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!