我想从部分过滤NSArray,但我的结果是空的。 请看下面的代码。
我有我的动物课。
@interface Animal : NSObject
{
int _id;
NSString *_name;
}
@property (nonatomic, assign) int id;
@property (nonatomic, copy) NSString *name;
- (id) initWithUniquedId:(int)strId strName:(NSString *)strName;
//实现Animal.m
- (id) initWithUniquedId:(int)strId strName:(NSString *)strName{
if(self = [super init]){
self.id = strId;
self.name = strName;
}
return self;
}
这是我从数据库获取数据的功能;
- (NSArray *)getAnimalInfo : (NSString *) argType{
// [...] Open database
NSMutableArray *data = [[NSMutableArray alloc] init];
NSString *query = [NSString stringWithFormat: @"SELECT * FROM animal WHERE type = %@ ", argType];
sqlite3_stmt *sqlstmt;
if(sqlite3_prepare_v2(database, [query UTF8Sring], -1, &sqlstmt, nil) == SQLITE_OK){
while(sqlite3_step(sqlstmt) == SQLITE_ROW){
int tmpId = sqlite3_column_int (sqlstmt, 0);
char *tmpName = (char *) sqlite3_column_text (sqlstmt, 1);
// Create object from class animal
Animal *am = [[Animal alloc] initWithUniquedId: tmpId strName: tmpName];
[data addObject: am];
}
sqlite3_finalize(sqlstmt);
}
// close database
return data;
}
在我的viewcontroller中,我已经实现了代码来获取所有动物信息。
- (void)viewDidLoad{
myData = [[NSArray alloc] init];
// There are many type of animal, so I need make it as section;
// This is what I show my tableview by section something like: poultry, dog, fish....
[myData addObject: [self getAnimalInfo: "poultry"]]; //type: poultry will contain many row
[myData addObject: [self getAnimalInfo: "dog"]]; //type: dog will contain many row
[myData addObject: [self getAnimalInfo: "fish"]]; //type: fish will contain many row
}
它们运作良好并且显示正常。
我想要的是从myData数组中按名称搜索这些信息。 看看我正在使用的代码是filterArrayUsingPredicate,但是没有结果。 我想知道如何从myData数组中搜索?
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name CONTAINS [cd] %@", myStringSearch];
self.myFilterAnimal = [myData filterArrayUsingPredicate:predicate;
NSLog(@"%@", self.myFilterAnimal];
//但没有结果。
抱歉我的英语不好。 感谢阅读。