2013-07-09 58 views
0

我有一個predicateWithFormat錯誤,導致我的應用程序崩潰。predicateWithFormat NSMutableArray問題

有問題的代碼是:

@implementation SecondViewController 
{ 
NSArray *searchResults; 
} 

@synthesize jsonConnection, bakeryProductArray, bakeryTableView; 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

// Do any additional setup after loading the view, typically from a nib. 
[self retrieveData]; 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

#pragma mark - UITableView DataSource 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return 1; 
} 


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
if (tableView == self.searchDisplayController.searchResultsTableView) { 
    return [searchResults count]; 
} 
else { 
return [bakeryProductArray count]; 

} 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath 
{ 
static NSString *cellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 
} 

if (tableView == self.searchDisplayController.searchResultsTableView) { 
    cell.textLabel.text = [searchResults objectAtIndex:indexPath.row]; 
} 
else { 
foodProducts *currentProduct = [bakeryProductArray objectAtIndex:indexPath.row]; 
cell.textLabel.text = currentProduct.productName; 
cell.detailTextLabel.text = currentProduct.productName; 
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
} 

return cell; 

} 

#pragma mark - UITableView Delegate methods 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if (bakeryTableView == self.searchDisplayController.searchResultsTableView) { 
    [self performSegueWithIdentifier: @"bakeryDetails" sender: self]; 
} 

    detailedViewController *productinfo = [self.storyboard instantiateViewControllerWithIdentifier:@"bakeryDetails"]; 
    //Retrieve current user array 
    foodProducts *currentProduct2 = [bakeryProductArray objectAtIndex:indexPath.row]; 
    productinfo.productName = currentProduct2.productName; 
    productinfo.productImage = currentProduct2.productImgUrl; 
    productinfo.productDescription = currentProduct2.productDescription; 
    productinfo.productPrice = currentProduct2.productPrice; 
    productinfo.productQuantity = currentProduct2.productquantity; 
    productinfo.productAllergy = currentProduct2.productAllergy; 

    [self.navigationController pushViewController:productinfo animated:YES]; 
    [bakeryTableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 


//Becareful here as wrong predicateWithFormat can crash app on keying in search 
-(void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope 
{ 
NSPredicate *resultPredicate = [NSPredicate predicateWithBlock:^BOOL(NSString * testString, NSDictionary *bindings) { 
    return ([searchText compare:testString options: NSCaseInsensitiveSearch] == NSOrderedSame); 
}]; 

searchResults = [bakeryProductArray filteredArrayUsingPredicate:resultPredicate]; 
} 


-(BOOL)searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchString:(NSString *)searchString 
{ 
[self filterContentForSearchText:searchString 
          scope:[[self.searchDisplayController.searchBar scopeButtonTitles] 
            objectAtIndex:[self.searchDisplayController.searchBar 
               selectedScopeButtonIndex]]]; 

return YES; 
} 


#pragma mark - methods 

-(void) retrieveData 
{ 
NSURL *url = [NSURL URLWithString:getDataUrl]; 
NSData *data = [NSData dataWithContentsOfURL:url]; 

jsonConnection = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 
bakeryProductArray = [[NSMutableArray alloc]init]; 

for (int i = 0; i < jsonConnection.count; i++) 
{ 
    NSString *pID = [[jsonConnection objectAtIndex:i] objectForKey:@"id"]; 
    NSString *pName = [[jsonConnection objectAtIndex:i] objectForKey:@"Name"]; 
    NSString *pImageUrl = [[jsonConnection objectAtIndex:i] objectForKey:@"ImageUrl"]; 
    NSString *pDescription = [[jsonConnection objectAtIndex:i] objectForKey:@"Description"]; 
    NSString *pQuantity = [[jsonConnection objectAtIndex:i] objectForKey:@"Quantity"]; 
    NSString *pPrice = [[jsonConnection objectAtIndex:i] objectForKey:@"Price"]; 
    NSString *pAllergy = [[jsonConnection objectAtIndex:i] objectForKey:@"Allergy"]; 

    foodProducts *myProducts = [[foodProducts alloc]initWithproductID:pID andproductName:pName andproductImgUrl:pImageUrl andproductDescription:pDescription andproductquantity:pQuantity andproductPrice:pPrice andproductAllergy:pAllergy]; 


    [bakeryProductArray addObject:myProducts]; 
} 


[self.bakeryTableView reloadData]; 

} 

FoodProduct類

@implementation foodProducts 
@synthesize productID, productName, productImgUrl, productquantity,productDescription, productPrice, productAllergy; 

-(id) initWithproductID:(NSString *)uproductID andproductName:(NSString *)uproductName andproductImgUrl:(NSString *)uproductImage andproductDescription:(NSString *)uproductDescription andproductquantity:(NSString *)uproductquantity andproductPrice:(NSString *)uproductPrice andproductAllergy:(NSString *)uproductAllergy 
{ 
self = [super init]; 
if (self) 
{ 
    productID = uproductID; 
    productName = uproductName; 
    productImgUrl = uproductImage; 
    productquantity = uproductquantity; 
    productDescription = uproductDescription; 
    productPrice = uproductPrice; 
    productAllergy = uproductAllergy; 

} 
return self; 
} 

正被送入NSPredicate陣列被@property (nonatomic, strong) NSMutableArray *bakeryProductArray;

錯誤說:不能使用/含有運營商與收集

任何想法,在wh ats出錯了?

+0

數組包含什麼,只是字符串?錯誤很明顯是什麼問題。 – Wain

+0

該數組攜帶NSStrings。 (我不是一個專業的程序員,所以對我來說很清楚就沒有意義)。 – Evilelement

+1

難道bakeryProductArray不是一個字符串數組,而是一個包含字符串屬性的*對象數組? - 你能顯示*完整的*錯誤信息嗎? –

回答

1

您的陣列不包含字符串,但自定義foodProducts對象。

,以過濾由「產品名稱」的對象,你會使用

[NSPredicate predicateWithFormat:@"productName CONTAINS[cd] %@", searchText]; 

新增:有一個在你的代碼中的另一個錯誤:

cell.textLabel.text = [searchResults objectAtIndex:indexPath.row]; 

不能因爲右手工作方不返回一個字符串。它應該是

foodProducts *currentProduct = [searchResults objectAtIndex:indexPath.row]; 
cell.textLabel.text = currentProduct.productName; 
+0

錯誤 - 此類不是關鍵字文本的關鍵字編碼兼容值。 – Evilelement

+0

@Evilelement:「text」只是一個例子*。您必須使用'foodProducts'的某個屬性,例如「productName」。 - 我根據您更新的問題更新了答案。 –

+0

彈跳,返回此類不是關鍵項目的關鍵值編碼。還是一個小菜。對不起 – Evilelement

0

NSPredicate * resultPredicate = [NSPredicate predicateWithFormat:@ 「self.productName包含苯並[cd]%@」,SEARCHTEXT];

NSArray * searchResults = [self.bakeryProductArray filteredArrayUsingPredicate:resultPredicate];

NSLog(@「%@」,searchResults);

如果上述不起作用,您可以顯示您的foodProduct類(模型)。

理想情況下,您應該使用[cd]而不是包含[cd]。

請參考:http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/Predicates/Articles/pSyntax.html 1 CONTAINS 左邊的表達式包含右邊的表達式。

2 LIKE 左手錶達式等於右手錶達式:?和*允許作爲通配符,在哪裏?匹配1個字符並*匹配0個或更多字符。

+0

將向你展示問題中的foodProduct類。 – Evilelement

+0

它不會崩潰:d但是....它不顯示任何結果:(控制檯每個字符2013年7月9日21讀取:45:53.737 Jackies [5537:C07]( 「 「 – Evilelement

+0

你嘗試self.productName,它並不雖然無所謂。@馬丁和我基本上都建議。我關注了他你們的談話同樣的事情。會後,如果發現任何東西。 – Anup