使用objectForKey返回错误类型的键

时间:2010-11-10 11:13:01

标签: iphone ipad

  • 我正在从NSDictionary数组的文件中读取数据。每个NSDictionary包含一个名为“EnglishName”的键,值为NSString的类型。我得到一个NSDictionary的值,用初始化函数创建新的ViewController:initWithName:[(NSDictionary *)oneObjectInArray objectForKey:@“EnglishName”];然后使用NavigationController来推送它。
  • 我第一次推它 - >没问题。然后按回然后再次执行(单击以创建新的ViewController并按下),出现错误,并显示:[__ NSArrayM rangeOfString:]:无法识别的选择器发送到实例0x63509a0'。 - 我通过添加命令NSLog(@“%@”,[object getObjectForKey:@“EnglishName”]) - >错误来检查。 然后我尝试再次在didShowView函数中读取数据(后面)。它没有任何错误。

以下是从文件中获取数据的代码。太长但可能有助于解决这个问题` - (void)getListStoriesAvailable {     [_header removeAllObjects];     SAFE_RELEASE(saveDataFromDataFile)     [storiesAvailableArray removeAllObjects];     NSString * digit = @“0123456789”;     NSString * ALPHA = @“ABCDEFGHIJKLMNOPQRSTUVWXYZ”;

for (int i = 0; i < 27; i++) 
{
    NSMutableArray * element = [[NSMutableArray alloc] init];
    [storiesAvailableArray addObject:element];
    [element release];
}
NSString * path = [NSString stringWithFormat: @"%@/data.ini",documentDirectory];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) 
{
    saveDataFromDataFile = [[NSMutableArray alloc] initWithContentsOfFile:path];
    for (int i = 0; i < [saveDataFromDataFile count]; i++)
        [[saveDataFromDataFile objectAtIndex:i] setObject:@"NO" forKey:@"Downloading"];
    for (int  i = 0; i < [saveDataFromDataFile count]; i++)
    {
        NSDictionary * theStory = [saveDataFromDataFile objectAtIndex:i];       
        NSString * word = [theStory objectForKey:@"EnglishName"];
        if ([word length] == 0) continue;

        NSString * firstCharacter = [[word substringToIndex: 1] uppercaseString];

        NSRange range;
        //The special case when the name of story start with a digit, add the story into "0-9" header and index
        range = [digit rangeOfString:firstCharacter];
        if (range.location != NSNotFound) 
        {
            [[storiesAvailableArray objectAtIndex:0] addObject:theStory];
            continue;
        }
        //Check the index of the first character
        range = [ALPHA rangeOfString:firstCharacter];
        if (range.location != NSNotFound) 
        {
            [[storiesAvailableArray objectAtIndex:(range.location + 1)] addObject:theStory];
            continue;
        }
    }
}
NSMutableArray * temp = [[NSMutableArray alloc] init];
for (int i = 0; i < [storiesAvailableArray count]; i++)
{
    if ([[storiesAvailableArray objectAtIndex:i] count] != 0)
    {
        [_header addObject:[ALPHA_ARRAY objectAtIndex:i]];
        [temp addObject:[storiesAvailableArray objectAtIndex:i]];
    }
}
[storiesAvailableArray removeAllObjects];
for (int i = 0; i < [temp count]; i++)
    [storiesAvailableArray addObject:[temp objectAtIndex:i]];
//storiesAvailableArray = temp;
[temp release];

} `

还有代码来获取NSDictionary

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

{     currentTable = tableView;     [tableView deselectRowAtIndexPath:indexPath animated:YES];     NSInteger row = [indexPath row];     NSDictionary *故事;     if(tableView == listStoriesAvailable)story = [[storiesAvailableArray objectAtIndex:[indexPath section]] objectAtIndex:row];

_storyDetail = [[StoryViewController alloc] initWithName:[story objectForKey:@"EnglishName"]];
[self.navigationController setDelegate:self];
[self.navigationController pushViewController:_storyDetail animated:YES];

}

那么问题是什么?请帮助!

1 个答案:

答案 0 :(得分:1)

你没有保留你的字典。

请编辑您的问题并添加创建NSDictionary的代码。