2011-02-01 25 views
1

我正在嘗試使用filteredArrayUsingPredicate和從.plist文件中的數據構建的數組。不知怎的,它似乎從來沒有過濾我的數組。filteredArrayUsingPredicate不工作

這是我的陣列是如何構建的:

DrillDownAppAppDelegate *AppDelegate = (DrillDownAppAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    self.tableDataSource = [AppDelegate.data objectForKey:@"Rows"]; 
    copyDataSource = [ tableDataSource mutableCopy]; 

,然後我的謂詞是這樣的,

NSString *searchFor = search.text; 
[tableDataSource release]; 
tableDataSource = [copyDataSource mutableCopy]; 
if ([searchFor length] > 0) { 
NSLog(@"array = %@",tableDataSource); 
NSPredicate *pred = [NSPredicate predicateWithFormat:@"Self beginswith[c] %@",searchFor]; 
    [tableDataSource filteredArrayUsingPredicate:pred]; 
} 

回答

6
  • -[NSArray filteredArrayUsingPredicate:]返回一個包含結果的新陣列
  • -[NSMutableArray filterUsingPredicate:]過濾器就地。

因此改爲使用以下:

[tableDataSource filterUsingPredicate:pred]; 
1

我認爲你需要引號一輪串您subsituting到predicateWithFormat:方法。即:

NSPredicate * pred = [NSPredicate predicateWithFormat:@「self beginswith [c] \」%@ \「」,searchFor];

也請嘗試使用「self」這個詞作爲所有小寫字母,如關鍵字「self」,並記住對象實例應該以小寫字母開頭。例如,「appDelegate」。