- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我环顾四周,但一直无法找到这个问题的答案......
我的应用程序中有一个包含汉语拼音发音的类,设置为发音# [空格] 发音#例如你好[你好]会是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/
本次和大家分享的是一个集成1:小写拼音 2:大写拼音 3:数字 4:汉字的验证码生成类,从标题来看感觉很普通的样子,没错的确很普通,只是这个验证码类生成的时候可以通过参数指定验证码返回格式的规则,更
我环顾四周,但一直无法找到这个问题的答案...... 我的应用程序中有一个包含汉语拼音发音的类,设置为发音# [空格] 发音#例如你好[你好]会是ni3 hao3 所以我的问题是如何让 NSPredi
是否可以 一个。找出一个字符是否是中文(简体),如果是中文 B. 得到拼音?例子: 你好 => nǐhǎo 使用 java 还是 php? 干杯 最佳答案 一) 是的。 Unicode 中表示的所有字
这里有个问题,我需要用golang获取一个中文单词的拼音,例如: 我想从中文“世界”中得到的是除了“世界”之外的字母“S”。 go 语言可以自动完成吗? 最佳答案 使用 utf8 包你可以做类似的事情
我是一名优秀的程序员,十分优秀!