Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone EXC_对重新加载数据的错误访问_Iphone_Objective C_Xcode_Macos - Fatal编程技术网

Iphone EXC_对重新加载数据的错误访问

Iphone EXC_对重新加载数据的错误访问,iphone,objective-c,xcode,macos,Iphone,Objective C,Xcode,Macos,我使用UISearchBar从API动态加载数据,并尝试使用如下方式显示数据: - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { [searchBar resignFirstResponder]; NSLog(@"Search Text: %@",[searchBar text]); [self startSearchingWithText:[searchBar text]]; } - (voi

我使用
UISearchBar
从API动态加载数据,并尝试使用如下方式显示数据:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    NSLog(@"Search Text: %@",[searchBar text]);
    [self startSearchingWithText:[searchBar text]];
}
- (void) startSearchingWithText:(NSString *)text {
    ...
    // Go through the list of services and set up a query for the search term
    QueryServiceManager *manager = [[QueryServiceManager alloc] initWithServices: services];

    // Set up the query
    if (![manager addKeyword: text])
        NSLog(@"PROBLEM!");

    // Execute the query and store the titles in an array
    self.data = [[manager makeQuery] copy]; // Returns a NSArray

    NSLog(@"%@",self.data);

    // Add the items to the tableView

    [self.tableView reloadData];
我的
UITableViewController
代码设置为从self.data读取,self.data最初是
@“
元素的
NSArray
,例如:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    [self.data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...

    cell.textLabel.text = [self.data objectAtIndex:indexPath.row];

    return cell;
}
当我这样做时,我得到的是:

(gdb)继续 2010-02-26 18:43:32.515我保存[11690:207]( “东芝卫星L505-GS5037 TruBrite 15.6英寸笔记本电脑(黑色)”, “Dell Inspiron 11.6英寸黑曜石黑色笔记本电脑(Windows 7 Premium)”, “华硕Eee PC Seashell 1005PE-MU17-BK 10.1英寸黑色上网本-电池寿命长达11小时”, “SwissGear电脑背包(红色)”, “Belkin 36件无符号电脑工具箱(黑色)”, “康柏EVO N610C”, “Belkin F8E062 55件套电脑工具包,带黑色外壳(消磁工具)”, “华硕Eee PC Seashell 1005PE-PU17-BK 10.1英寸黑色上网本-电池寿命长达14小时”, “Harman Kardon SoundSticks II 2.1即插即用多媒体扬声器系统”, “ION Audio VCR 2 PC USB VHS视频到计算机转换器” ) (gdb)继续 程序收到信号:“EXC\U坏访问”。 (gdb)

有什么想法吗?看起来,
NSArray
已正确填充,然后在重新加载数据调用时/之后出现故障(断点确认了这一点,但无法隔离哪里出了问题)

编辑:我用枚举替换了
NSLog()

for ( NSString *elem in self.data ) 
    NSLog(elem);
它仍然崩溃。我设法哄出了一个日志:


当我完全删除
NSLog()
时,它不会崩溃,但是
UITableView
也不会更新。

奇怪的是:你是说每个部分只有一行,而你却在使用
indexPath.row
索引到数组中?其中一个是错误的,我猜这是第一个。通常,如果在
UITableView
中显示一个数组(由n个元素组成),则该节中有一个节和n行,这意味着您将使用
[myArray objectAtIndex:indexPath.row]
检索适当的对象

如果要为数组中的每个项指定一个单独的节,则可以说每个节中有n个节和1行,然后使用
[myArray objectAtIndex:indexath.section]
检索相应的对象


哦,而且你到处都在泄漏内存。

奇怪的是:你说每个部分只有一行,但是你在使用
indexath.row
索引到数组中?其中一个是错误的,我猜这是第一个。通常,如果在
UITableView
中显示一个数组(由n个元素组成),则该节中有一个节和n行,这意味着您将使用
[myArray objectAtIndex:indexPath.row]
检索适当的对象

如果要为数组中的每个项指定一个单独的节,则可以说每个节中有n个节和1行,然后使用
[myArray objectAtIndex:indexath.section]
检索相应的对象

哦,而且你到处都在泄漏内存。

那么:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.data count];
}
不要总是返回
1

那么:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.data count];
}

不是总是返回
1

您是否识别了您试图访问但已不存在的对象,即给予EXC\u BAD\u访问权限的对象


如果没有,那么你应该在你的应用程序中启用僵尸,然后你可以向gdb发出一个命令,告诉你是哪个对象导致了崩溃。

你是否识别了你试图访问但已不存在的对象,即给EXC\u BAD\u访问权限的对象


如果没有,那么您应该在应用程序中启用僵尸,然后您可以向gdb发出命令,告诉您是哪个对象导致了崩溃。

点击shift-command-b分析项目并解决显示的所有问题(潜在泄漏等)。

点击shift-command-b分析项目并解决显示的所有问题(潜在泄漏等)

您在哪里调用reloadData?在上面的NSLog()命令下。当点击“搜索”按钮时会自动调用该函数。后编辑以显示完整定义。日志中说什么通常有一种方法导致“EXC\u坏访问”我在上面粘贴了gdb输出。没有更多的信息了。在系统控制台而不是调试控制台中呢?崩溃日志报告?您在哪里调用重载数据?在上面的NSLog()命令下。该函数在“搜索”时自动调用点击按钮。后编辑以显示完整定义。日志中说什么通常有一种方法导致“EXC\u错误访问”我在上面粘贴了gdb输出。没有更多的信息了。那么在系统控制台而不是调试控制台中呢?崩溃日志报告?这不是问题;我让它返回1,这样我就可以确保它在不经过整个过程的情况下工作。如果它对1有效,则更容易使它对整个阵列工作。我刚刚编辑了代码将反映“正确”计数,但同样的问题也会出现。这不是问题;我让它返回1,这样我就可以确保它在不经过整个过程的情况下工作。如果它对1有效,则更容易使它对整个数组起作用。我只是编辑了代码以反映“正确”计数,但同样的问题也出现了。像下面的人一样,为了测试的目的,我在那里只有“return 1;”。我将编辑上面的代码以反映这一点,并将正确的代码([self.data count])放入其中,但不管怎样,这都是同一个问题。像下面的人一样,我只有“return 1;”出于测试目的,我将编辑上面的代码以反映这一点,并将正确的代码([self.data count])放入其中,但不管怎样,这都是相同的问题。