下面的代码示例显示了如何通过带有字典的数组的plist中的“Name”字符串进行搜索。现在我在视图中添加了两个UISliders:sliderOne和sliderTwo。我想在searchButtonPressed中使用它们作为第二个critera,意思是:
resultObjectsArray现在包含objectName包含nameString的对象(如示例所示)AND objectPrice介于sliderOne和sliderTwo之间。 但是,如果sliderOne的值高于滑块2的值,请跳过此条件。
使用下面的示例,您能告诉我如何做到这一点吗?价格是plist字典中的数字(真实),如“99,90”。
-(IBAction)searchButtonPressed:(id)sender{
NSString *path = [[NSBundle mainBundle] pathForResource:@"Objects" ofType:@"plist"];
allObjectsArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSString *nameString = [NSString stringWithFormat:@"%@", [nameTextField text]];
resultObjectsArray = [NSMutableArray array];
for(NSDictionary *object in allObjectsArray)
{
NSString *objectName = [object objectForKey:@"Name"];
NSString *objectPrice = [wine objectForKey:@"Price"];
NSRange range = [objectName rangeOfString:nameString options:NSCaseInsensitiveSearch];
if(range.location != NSNotFound)
[resultObjectsArray addObject:object];
}
谢谢!
答案 0 :(得分:1)
我想我理解你的问题,在这种情况下改变条件
是不够的if (range.location != NSNotFound)
[resultObjectsArray addObject:object];
与
BOOL priceConditionGood = YES;
if (sliderOne.value <= sliderTwo.value && (objectPrice.floatValue < sliderOne.value || objectPrice.floatValue > sliderTwo.value))
priceConditionGood = NO;
if (range.location != NSNotFound && priceConditionGood)
[resultObjectsArray addObject:object];
请告诉我这是否适合您!