2012-08-02 60 views
0

下面的代碼示例顯示瞭如何通過字典中的數組與字符串中的「Name」字符串進行搜索。現在我在視圖中添加了兩個UISliders:sliderOne和sliderTwo。我想在searchButtonPressed時使用它們作爲第二個標準符號,意思是:在我的searchButtonPressed中添加第二個標記使用XCode

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]; 
    } 

謝謝!

回答

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]; 

讓我知道,如果你是這樣的!

+0

像魔術一樣工作,非常感謝!你知道任何優秀的教程或學習製作這樣的功能的東西嗎? – ingenspor 2012-08-02 23:34:08

相關問題