作成した plist で文字を検索すると、検索バーがクラッシュします。文字が存在しなくてもクラッシュしません。何がうまくいかないのかわかりません。誰かが助けてくれれば幸いです、ありがとう。
コードはこちら
viewController.h
@interface ViewController : UITableViewController <UISearchDisplayDelegate,
UISearchBarDelegate>
@property (strong, nonatomic) NSArray *authors;
@property (strong, nonatomic) NSMutableArray *searchResults;
@property IBOutlet UISearchBar *searchBar;
@end
viewController.m
#import "ViewController.h"
#import "MTBookCoverViewController.h"
#import "MTBooksViewController.h"
#import "Cell.h"
@interface ViewController ()
@end
@implementation ViewController{
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Set Title
self.title = @"Authors";
// Load Property List
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Books" ofType:@"plist"];
self.authors = [NSArray arrayWithContentsOfFile:filePath];
NSLog(@"authors > %@", self.authors);
self.searchResults = [NSMutableArray arrayWithCapacity:[self.authors count]];
}
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
[self.searchResults removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
self.searchResults = [NSMutableArray arrayWithArray:[self.authors filteredArrayUsingPredicate:predicate]];
[[self tableView] reloadData];
}
#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// Tells the table data source to reload when text changes
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex: [self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
// Tells the table data source to reload when scope bar selection changes
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
//return 0;
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [self.searchResults count];
} else {
return [self.authors count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"RecipeCell";
static NSString *CellIdentifier = @"Cell";
Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (Cell == nil) {
Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
Cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
} else {
//from the plist
NSDictionary *author = [self.authors objectAtIndex:[indexPath row]];
[cell.label setText:[author objectForKey:@"Author"]];
[cell.label2 setText:[author objectForKey:@"Cover"]];
cell.icon.image = [UIImage imageNamed:[author objectForKey:@"Cover"]];
}
return cell;
}
#pragma mark -
#pragma mark Table View Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Initialize Books View Controller
MTBooksViewController *booksViewController = [[MTBooksViewController alloc] init];
// Fetch and Set Author
NSDictionary *author = [self.authors objectAtIndex:[indexPath row]];
[booksViewController setAuthor:[author objectForKey:@"Author"]];
// Push View Controller onto Navigation Stack
[self.navigationController pushViewController:booksViewController animated:YES];
}
@end
これがplistです
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>name</key>
<string>w</string>
<key>Author</key>
<string>J.R.R. Tolkien</string>
<key>Cover</key>
<string>twitter.png</string>
<key>Books</key>
<array>
<dict>
<key>name</key>
<string>test</string>
<key>Cover</key>
<string>twitter.png</string>
<key>Title</key>
<string>The Fellowship of the Ring</string>
<key>Year</key>
<string>1954</string>
</dict>
<dict>
<key>Cover</key>
<string>two-towers.jpg</string>
<key>Title</key>
<string>The Two Towers</string>
<key>Year</key>
<string>1954</string>
</dict>
<dict>
<key>Cover</key>
<string>return-king.jpg</string>
<key>Title</key>
<string>The Return of the King</string>
<key>Year</key>
<string>1955</string>
</dict>
<dict>
<key>Cover</key>
<string>bilbo-song.png</string>
<key>Title</key>
<string>Bilbo's Last Song</string>
<key>Year</key>
<string>1966</string>
</dict>
</array>
</dict>
<dict>
<key>name</key>
<string>q</string>
<key>Author</key>
<string>Dan Brown</string>
<key>Cover</key>
<string>fellowship.jpg</string>
<key>Books</key>
<array>
<dict>
<key>Cover</key>
<string>digital-fortress.jpg</string>
<key>Title</key>
<string>Digital Fortress</string>
<key>Year</key>
<string>1998</string>
</dict>
<dict>
<key>Cover</key>
<string>angels-demons.jpg</string>
<key>Title</key>
<string>Angels and Demons</string>
<key>Year</key>
<string>2000</string>
</dict>
<dict>
<key>Cover</key>
<string>deception-point.jpg</string>
<key>Title</key>
<string>Deception Point</string>
<key>Year</key>
<string>2001</string>
</dict>
<dict>
<key>Cover</key>
<string>da-vinci-code.jpg</string>
<key>Title</key>
<string>The Da Vinci Code</string>
<key>Year</key>
<string>2003</string>
</dict>
<dict>
<key>Cover</key>
<string>lost-symbol.jpg</string>
<key>Title</key>
<string>The Lost Symbol</string>
<key>Year</key>
<string>2009</string>
</dict>
</array>
</dict>
<dict>
<key>name</key>
<string>w</string>
<key>Author</key>
<string>J.K. Rowling</string>
<key>Cover</key>
<string>fellowship.jpg</string>
<key>Books</key>
<array>
<dict>
<key>Cover</key>
<string>stone.jpg</string>
<key>Title</key>
<string>Harry Potter and the Philosopher's Stone</string>
<key>Year</key>
<string>1997</string>
</dict>
<dict>
<key>Cover</key>
<string>chamber.jpg</string>
<key>Title</key>
<string>Harry Potter and the Chamber of Secrets</string>
<key>Year</key>
<string>1998</string>
</dict>
<dict>
<key>Cover</key>
<string>azkaban.jpg</string>
<key>Title</key>
<string>Harry Potter and the Prisoner of Azkaban</string>
<key>Year</key>
<string>1999</string>
</dict>
<dict>
<key>Cover</key>
<string>goblet.jpg</string>
<key>Title</key>
<string>Harry Potter and the Goblet of Fire</string>
<key>Year</key>
<string>2000</string>
</dict>
<dict>
<key>Cover</key>
<string>phoenix.jpg</string>
<key>Title</key>
<string>Harry Potter and the Order of the Phoenix</string>
<key>Year</key>
<string>2003</string>
</dict>
<dict>
<key>Cover</key>
<string>prince.jpg</string>
<key>Title</key>
<string>Harry Potter and the Half Blood Prince</string>
<key>Year</key>
<string>2005</string>
</dict>
<dict>
<key>Cover</key>
<string>hallows.jpg</string>
<key>Title</key>
<string>Harry Potter and the Deathly Hallows</string>
<key>Year</key>
<string>2007</string>
</dict>
<dict>
<key>Cover</key>
<string>bard.jpg</string>
<key>Title</key>
<string>The Tales of Beedle the Bard</string>
<key>Year</key>
<string>2008</string>
</dict>
</array>
</dict>
<dict>
<key>name</key>
<string>s</string>
<key>Author</key>
<string>Robin Cook</string>
<key>Cover</key>
<string>fellowship.jpg</string>
<key>Books</key>
<array>
<dict>
<key>Cover</key>
<string>sphinx.jpeg</string>
<key>Title</key>
<string>Sphinx</string>
<key>Year</key>
<string>1979</string>
</dict>
<dict>
<key>Cover</key>
<string>brain.jpg</string>
<key>Title</key>
<string>Brain</string>
<key>Year</key>
<string>1981</string>
</dict>
<dict>
<key>Cover</key>
<string>fever.jpg</string>
<key>Title</key>
<string>Fever</string>
<key>Year</key>
<string>1982</string>
</dict>
<dict>
<key>Cover</key>
<string>blindsight.png</string>
<key>Title</key>
<string>Blindsight</string>
<key>Year</key>
<string>1992</string>
</dict>
<dict>
<key>Cover</key>
<string>fatal-cure.jpeg</string>
<key>Title</key>
<string>Fatal Cure</string>
<key>Year</key>
<string>1994</string>
</dict>
<dict>
<key>Cover</key>
<string>invasion.jpg</string>
<key>Title</key>
<string>Invasion</string>
<key>Year</key>
<string>1997</string>
</dict>
<dict>
<key>Cover</key>
<string>shock.jpg</string>
<key>Title</key>
<string>Shock</string>
<key>Year</key>
<string>2001</string>
</dict>
<dict>
<key>Cover</key>
<string>intervention.jpg</string>
<key>Title</key>
<string>Intervention</string>
<key>Year</key>
<string>2009</string>
</dict>
</array>
</dict>
</array>
</plist>
これは私が得る例外です:
2013-10-23 06:22:44.111 tabnav[20102:a0b] -[__NSCFDictionary length]: unrecognized selector sent to instance 0x109374b60
2013-10-23 06:22:44.181 tabnav[20102:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary length]: unrecognized selector sent to instance 0x109374b60'
*** First throw call stack:
(
0 CoreFoundation 0x0000000101895795 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001015f8991 objc_exception_throw + 43
2 CoreFoundation 0x0000000101926bad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010188709d ___forwarding___ + 973
4 CoreFoundation 0x0000000101886c48 _CF_forwarding_prep_0 + 120
5 Foundation 0x00000001011efb6b -[NSConcreteMutableAttributedString replaceCharactersInRange:withString:] + 46
6 Foundation 0x00000001011f0646 -[NSConcreteMutableAttributedString initWithString:attributes:] + 249
7 UIKit 0x00000001003d38a7 -[UILabel _setText:] + 85
8 tabnav 0x0000000100004c8a -[ViewController tableView:cellForRowAtIndexPath:] + 506
9 UIKit 0x000000010032c13a -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 348
10 UIKit 0x0000000100313fc6 -[UITableView _updateVisibleCellsNow:] + 2297
11 UIKit 0x000000010031e728 -[UITableView _visibleCells] + 29
12 UIKit 0x0000000100325c12 -[UITableView _scrollFirstResponderCellToVisible:] + 82
13 UIKit 0x000000010032ea59 -[UITableView _adjustForAutomaticKeyboardInfo:animated:lastAdjustment:] + 88
14 UIKit 0x0000000100583cf7 -[UISearchDisplayController _setTableViewVisible:inView:] + 895
15 UIKit 0x0000000100261a8a -[UIApplication sendAction:to:from:forEvent:] + 104
16 UIKit 0x0000000100261a20 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17
17 UIKit 0x000000010033591c -[UIControl _sendActionsForEvents:withEvent:] + 203
18 UIKit 0x000000010086bfd4 -[UITextField fieldEditorDidChange:] + 198
19 UIKit 0x0000000100877660 -[UITextInputController _sendDelegateChangeNotificationsForText:selection:] + 102
20 UIKit 0x0000000100878e85 -[UITextInputController _insertText:fromKeyboard:] + 450
21 UIKit 0x0000000100879652 -[UITextInputController insertText:] + 301
22 UIKit 0x00000001003fd8e0 -[UIKeyboardImpl insertText:] + 82
23 Foundation 0x000000010126f9e4 -[__NSOperationInternal _start:] + 623
24 UIKit 0x00000001003fc387 -[UIKeyboardImpl performOperations:] + 148
25 UIKit 0x00000001003fa9b7 __73-[UIKeyboardImpl replyHandlerForHandleKeyboardInputWithExecutionContext:]_block_invoke_2 + 40
26 UIKit 0x000000010088d9d6 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 329
27 Foundation 0x00000001011fe6f7 __NSThreadPerformPerform + 227
28 CoreFoundation 0x0000000101824e61 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
29 CoreFoundation 0x0000000101824732 __CFRunLoopDoSources0 + 242
30 CoreFoundation 0x00000001018405bf __CFRunLoopRun + 767
31 CoreFoundation 0x000000010183fed3 CFRunLoopRunSpecific + 467
32 GraphicsServices 0x00000001039763a4 GSEventRunModal + 161
33 UIKit 0x0000000100260a63 UIApplicationMain + 1010
34 tabnav 0x0000000100002643 main + 115
35 libdyld.dylib 0x0000000101f237e1 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)