gpt4 book ai didi

ios - NSPredicate 忽略字符串中的数字 [拼音]

转载 作者:行者123 更新时间:2023-11-29 03:14:19 26 4
gpt4 key购买 nike

我环顾四周,但一直无法找到这个问题的答案......

我的应用程序中有一个包含汉语拼音发音的类,设置为发音# [空格] 发音#例如你好[你好]会是ni3 hao3

所以我的问题是如何让 NSPredicate 在搜索过程中忽略字符串/类中的数字?

理想情况下,我将能够搜索:“你好”“你好”等等

结果还是一样(你好ni3 hao3)

我已经尝试了几个 LIKE 实例,但每次都惨败......

谢谢

根据要求,这是我的数组:

     -(NSMutableArray *) wordList{
cdh = [[NSMutableArray alloc] initWithCapacity:10];
@try {
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"cdh.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured: %s", sqlite3_errmsg(db));

}


const char *sql = "SELECT * FROM MAIN";
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db));
}else{

while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
Words * word = [[Words alloc] init];
word.head = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,0)];
word.pro = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
word.def = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)];
[cdh addObject:word];
}
}
sqlite3_finalize(sqlStatement);
}
@catch (NSException *exception) {
NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db));
}
@finally {
sqlite3_close(db);
return cdh;

}
}

Words.h

#import <Foundation/Foundation.h>
#import "TestViewController.h"

@interface Words : NSObject {
NSString *head;
NSString *pro;
NSString *def;
}

@property(nonatomic, copy) NSString *head;
@property(nonatomic, copy) NSString *pro;
@property(nonatomic, copy) NSString *def;

@end

Words.m

#import "Words.h"

@implementation Words

@synthesize head;
@synthesize pro;
@synthesize def;

- (NSString *)searchableStringValue {
NSCharacterSet *invalidSet = [[NSCharacterSet characterSetWithCharactersInString:@"[]0123456789 "] invertedSet];
NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:@""];
return searchString;
}

@end

TestViewController.h

#import <UIKit/UIKit.h>
#import <sqlite3.h>
#import "Words.h"

@interface TestViewController : UITableViewController {
NSMutableArray *cdh;
sqlite3 * db;
}
@property(nonatomic,retain) NSMutableArray *cdh;
@property (nonatomic, strong) NSData *myData;

-(NSMutableArray *) wordList;

@end

TestViewController.m(包括谓词)

    - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"(head beginswith[c] %@) OR (searchableStringValue beginswith[c] %@)", searchText, searchText];
searchResults = [cdh filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];

return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [self.cdh count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// int rowCount = indexPath.row;

// Words *word = [self.cdh objectAtIndex:rowCount];
// cell.textLabel.text = word.head;
// cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", word.pro, word.def];

// Configure the cell...
Words *word = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
word = [searchResults objectAtIndex:indexPath.row];
} else {
word = [cdh objectAtIndex:indexPath.row];
}

cell.textLabel.text = word.head;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", word.pro, word.def];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"ShowWordDetails"]) {
NSIndexPath *indexPath = nil;
Words *word = nil;

if (self.searchDisplayController.active) {
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
word = [searchResults objectAtIndex:indexPath.row];
} else {
indexPath = [self.tableView indexPathForSelectedRow];
word = [cdh objectAtIndex:indexPath.row];
}

DetailsViewController *destViewController = segue.destinationViewController;
destViewController.word = word;
}
}

最佳答案

您的字符串包含在 Words 类中。这为您提供了一个绝佳的机会来创建额外的方法来帮助处理它包含的字符串(即您不需要尝试在谓词中做所有事情)。

例如,考虑添加一个返回的方法:

- (NSString *)searchableStringValue

此方法将采用您当前尝试搜索的字符串并对其进行变异以删除括号、数字和空格。这可以最容易地通过以下方式实现:

NSCharacterSet *invalidSet = [[NSCharacterSet characterSetWithCharactersInString:@"[]0123456789 "] invertedSet];
NSString *searchString = [[##XXX## componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:@""];

其中 ##XXX## 是您当前尝试搜索的字符串。

现在,您的谓词应该使用 searchableStringValue,而不是您当前尝试搜索的字符串。

关于ios - NSPredicate 忽略字符串中的数字 [拼音],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21852502/

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