我使用一個plist文件以獲得其顯示在tableview中問題與NSMutableArray的和valueforkey
plist中看起來像這個網站的列表:
<array>
<dict>
<key>site</key>
<string>http://yahoo.com</string>
<key>title</key>
<string>Yahoo</string>
</dict>
<dict>
<key>site</key>
<string>http://google.com</string>
<key>title</key>
<string>Google</string>
</dict>
//...etc
</array>
</plist>
我沒有問題顯示此:
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TestData" ofType:@"plist"]];
[self setContentsList:array];
* 問題是當我嘗試搜索內容時,我想從搜索結果中獲取valueforkey @「site」以使用它在didSelectRowAtIndexPath方法:*
NSMutableArray *contentsList;
NSMutableArray *searchResults;
NSString *savedSearchTerm;
---------------------
- (void)handleSearchForTerm:(NSString *)searchTerm
{
[self setSavedSearchTerm:searchTerm];
if ([self searchResults] == nil)
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[self setSearchResults:array];
[array release], array = nil;
}
[[self searchResults] removeAllObjects];
if ([[self savedSearchTerm] length] != 0)
{
for (NSString *currentString in [[self contentsList] valueForKey:@"title"])
{
if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
{
[[self searchResults] addObject:currentString];
// NSDictionary *dic= [[NSDictionary alloc]allKeysForObject:searchResults];
}
}
}
}
的didSelectRowAtIndexPath方法用於在網頁視圖中打開該網站
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *arraySite = [[[self searchResults] objectAtIndex:indexPath.row] valueForKey:@"site"];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:arraySite]]];
[self performSelector:@selector(showSearch:) withObject:nil afterDelay:0];
}
錯誤,我得到:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSCFString 0x6042730> valueForUndefinedKey:]: this class is not key value coding-compliant for the key site.'
驚人!!有效!謝謝你的解釋。 – Alby 2011-04-28 01:57:09