Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.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 如何在NSDictionary的UITableView单元格中显示数据?_Iphone - Fatal编程技术网

Iphone 如何在NSDictionary的UITableView单元格中显示数据?

Iphone 如何在NSDictionary的UITableView单元格中显示数据?,iphone,Iphone,您好,我收到了来自udp的2000个数据,并在tableview中显示这些值。最简单的方法是什么 现在我使用两个NSThread和一个线程通过udp接收数据并将其存储在NSMutableDictionary中。另一个线程使用这些字典值更新tableview。但它使我的应用程序崩溃了 下面是我使用的一些代码 我像这样存储收到的值 NSMutableDictionary *dictItem CustomItem *item = [[CustomItem alloc]init]; item.SN

您好,我收到了来自udp的2000个数据,并在tableview中显示这些值。最简单的方法是什么

现在我使用两个NSThread和一个线程通过udp接收数据并将其存储在NSMutableDictionary中。另一个线程使用这些字典值更新tableview。但它使我的应用程序崩溃了

下面是我使用的一些代码

我像这样存储收到的值

 NSMutableDictionary *dictItem
 CustomItem *item = [[CustomItem alloc]init];
 item.SNo =[NSString stringWithFormat:@"%d",SNo];
 item.Time=CurrentTime;
 [dictItem setObject:item forKey:[NSString stringWithFormat:@"%d",SNo]];
 [item release];
我使用委托方法和CustomTableCells将数据显示为列

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

错误是

由于未捕获的异常“NSGenericeException”而终止应用程序,原因:“***集合在枚举时发生了变异。” 2010-07-23 02:33:07.891 Centrak[2034:207]堆栈:( 42162256, 43320108, 42161198, 43372629, 41719877, 41719345, 9948, 3276988, 3237662, 3320232, 3288478, 71153942, 71153189, 71096786, 71096114, 71296742, 41650770, 41440069, 41437352, 51148957, 51149154, 2925426 ) 在抛出“NSException”实例后调用terminate

有人能帮我吗


提前感谢……

您可能必须使用锁定,因为当您从表视图访问字典时,它可能会与其他线程发生变异。 试着看看文档。在修改字典之前,请执行
[myLock lock]和变异后的do
[myLock unlock]。在其他线程中类似:在枚举字典之前执行
[myLock lock]获取所有值后执行
[myLock unlock]
myLock
是一个NSLock对象,必须在线程之间共享。

可变集合本质上不是线程安全的,因此如果将它们用于多个线程,则必须首先创建一个不可变副本。例如,如果您想遍历NSMutableDictionary中的所有键,您可以这样做(假设您的
NSMutableDictionary
被称为
mutableDictionary
):

如果您不想复制字典,我想您可以使用锁或简单地使用
@synchronized
指令,如下所示:

@synchronized(mutableDictionary) {
  // Do anything you want to be thread-safe here.
}

有关更多信息,请查看有关多线程和线程安全对象的Apple文档:

您提供的信息无法帮助您。如果您可以为表视图数据源委托方法发布一些代码,从字典中提取数据并将其放入单元格中。
NSDictionary *dictionary = [NSDictionary dictionaryWithDictionary:mutableDictionary];

for(id key in dictionary) {
  // Do anything you want to be thread-safe here.
}
@synchronized(mutableDictionary) {
  // Do anything you want to be thread-safe here.
}