的问题是解决了,这里是我如何固定它以供将来参考的菜鸟和我一样 我需要删除空白频段: ship.countryOfbuild = [[elements objectAtIndex:0] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
通过对象的NSMutableArray与NSPredicate搜索
我不得不改变这样的断言:NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"(countryOfBuild contains[cd] %@) OR (shipBuilder contains[cd] %@) OR (name contains[cd] %@) OR (owner contains[cd] %@)",searchText, searchText, searchText, searchText];
原来的问题
我使用this tutorial到搜索栏添加到我的项目。我得到了所有的工作表,但搜索功能崩溃的应用程序。我现在的搜索方式是:
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
searchResults = [searchContent filteredArrayUsingPredicate:resultPredicate];
}
的searchContent
是可变数组从.csv文件是这样写着:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString* pathToFile = [[NSBundle mainBundle] pathForResource:@"ships" ofType: @"csv"];
NSString *file = [[NSString alloc] initWithContentsOfFile:pathToFile encoding: NSUTF8StringEncoding error:NULL];
NSArray *allLines = [file componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
searchContent = [[NSMutableArray alloc] init];
for (NSString* line in allLines) {
NSArray *elements = [line componentsSeparatedByString:@";"];
CargoShips* ship = [[CargoShips alloc]init];
ship.countryOfbuild = [elements objectAtIndex:0];
ship.shipBuilder = [elements objectAtIndex:1];
ship.hullHashtag = [elements objectAtIndex:2];
ship.name = [elements objectAtIndex:3];
ship.owner = [elements objectAtIndex:4];
ship.shipOperator = [elements objectAtIndex:5];
ship.shipDelivery = [elements objectAtIndex:6];
ship.shipFlag = [elements objectAtIndex:7];
ship.shipClass = [elements objectAtIndex:8];
ship.powerPlant = [elements objectAtIndex:9];
ship.HP = [elements objectAtIndex:10];
ship.speed = [elements objectAtIndex:11];
ship.cargoSystem = [elements objectAtIndex:12];
ship.numberOfTanks = [elements objectAtIndex:13];
ship.size = [elements objectAtIndex:14];
[self.searchContent addObject:ship];
}
我想搜索通过所有15个的搜索对象(名称,大小,shipclass等)
目前,这个崩溃与错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use in/contains operator with collection <CargoShips: 0x714e1b0> (not a collection)'
CargoShips是我的NSObject类。
如何修复NSPredicate以通过NSMutableArray
搜索没有崩溃的对象?
以下是完整的代码,如果让东西更容易http://pastebin.com/sw12GwHK
这很棒,它至少不会崩溃。但1)搜索总是返回空白。 (在日志和屏幕上)和2)如何使它搜索全部15个字段?谢谢:) –
@OscarApeland,首先你需要找出为什么这不起作用。你需要做一些日志记录。记录self.searchContent以确保它具有您想要的内容。然后尝试记录self.searchContent [0] [@“shipBuilder”],并查看它是否会返回所期望的结果。 – rdelmar
Logging searchContent为我提供了很多'“”, “”, “”,“这些东西。大约450,我的.csv数据源中的行数。 self.searchContent [0] [@“shipBuilder”]使用[[CargoShips objectForKeyedSubscript:]:无法识别的选择器发送到实例0xb9e9370 2013-01-31 19:27:57.323 LNG [35488:c07]导致应用程序崩溃***由于未捕获异常'NSInvalidArgumentException',错误代码而终止应用程序。 –