【问题标题】:Use objectForKey return wrong type of key使用 objectForKey 返回错误类型的键
【发布时间】:2010-11-10 11:13:00
【问题描述】:
  • 我正在从 NSDictionary 数组的文件中读取数据。每个 NSDictionary 都包含一个名为“EnglishName”的键,一个值为 NSString 类型的值。我得到一个 NSDictionary 的值,用初始化函数创建新的 ViewController,比如:initWithName:[(NSDictionary*)oneObjectInArray objectForKey:@"EnglishName"];然后使用 NavigationController 推送它。
  • 第一次推送->没问题。然后按回然后再做一次(单击以创建新的 ViewController 并推送)出现错误并通知:[__NSArrayM rangeOfString:]: unrecognized selector sent to instance 0x63509a0'。 -我通过添加命令 NSLog(@"%@", [object getObjectForKey:@"EnglishName"])->error 进行了检查。 然后我尝试在 didShowView 函数中再次读取数据(返回后)。它可以正常工作。

这是从文件中获取数据的代码。太长但可能有助于解决这个问题`- (void) getListStoriesAvailable { [_header removeAllObjects]; SAFE_RELEASE(saveDataFromDataFile) [storiesAvailableArray removeAllObjects]; NSString * 数字 = @"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 

{ 当前表 = 表视图; [tableView deselectRowAtIndexPath:indexPath 动画:YES]; NSInteger 行 = [indexPath 行]; 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];

}

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

【问题讨论】:

    标签: iphone ipad


    【解决方案1】:

    你没有保留你的字典。

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

    【讨论】:

    • 我通过 [[NSArray alloc] initWithContentOfFile] 读取了一个 NSDictionary 数组。之后每次,我通过 [array objectAtIndex :pos] 得到 NSDictionary 然后按照我的描述做一些事情
    • 您肯定在某处丢失了字典,但您的代码看起来不错 - 我认为我们确实需要代码来提供帮助;你能在一个更小的测试应用中重现这个问题,以便我们能看到更多吗?
    • 在你的视图控制器的 dealloc 方法中你释放你的字典 - 你不应该因为[array objectAtIndex:pos]没有为你保留它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多