1

我正在从字典值数组中加载 UItableview 中的一些值。然后我通过添加另一个键值对象来更改数组中的字典,如下所示

NSMutableDictionary *rowDict = [tableList objectAtIndex:arrayindex];
[rowDict setObject:@"download successfull" forKey:@"downloadstatus"];

但是在此之后,当我尝试从数组中的字典中检索值时,如下所示

NSMutableDictionary *rowDict = [tableList objectAtIndex:arrayindex];
NSString *SelectedState = (NSString*)[rowDict objectForKey:@"downloadstatus"];

它崩溃了......任何人都可以帮我解决这个问题

这是我控制台上的崩溃显示

 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableData objectForKey:]: unrecognized selector sent to instance 0x61a8270'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x003b0be9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x015c15c2 objc_exception_throw + 47
    2   CoreFoundation                      0x003b26fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00322366 ___forwarding___ + 966
    4   CoreFoundation                      0x00321f22 _CF_forwarding_prep_0 + 50
    5   SifyMyStorage                       0x0003b35b -[DownloadListViewController tableView:cellForRowAtIndexPath:] + 314
    6   UIKit                               0x00ec67fa -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634
    7   UIKit                               0x00ebc77f -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
    8   UIKit                               0x00ed1450 -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
    9   UIKit                               0x00ec9538 -[UITableView layoutSubviews] + 242
    10  QuartzCore                          0x009f4451 -[CALayer layoutSublayers] + 181
    11  QuartzCore                          0x009f417c CALayerLayoutIfNeeded + 220
    12  QuartzCore                          0x009ed37c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
    13  QuartzCore                          0x009ed0d0 _ZN2CA11Transaction6commitEv + 292
    14  QuartzCore                          0x00a1d7d5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
    15  CoreFoundation                      0x00391fbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
    16  CoreFoundation                      0x003270e7 __CFRunLoopDoObservers + 295
    17  CoreFoundation                      0x002efbd7 __CFRunLoopRun + 1575
    18  CoreFoundation                      0x002ef240 CFRunLoopRunSpecific + 208
    19  CoreFoundation                      0x002ef161 CFRunLoopRunInMode + 97
    20  GraphicsServices                    0x02ead268 GSEventRunModal + 217
    21  GraphicsServices                    0x02ead32d GSEventRun + 115
    22  UIKit                               0x00e6142e UIApplicationMain + 1160
    23  SifyMyStorage                       0x000020b8 main + 102
    24  SifyMyStorage                       0x00002049 start + 53
)
terminate called after throwing an instance of 'NSException'
4

3 回答 3

0
See if you have allocated the object and also get the count of dictionary , if the value is being added also see if u have declared NSmutabledictionary or just NSdictionary ... a view at your class having the code would be more helpful to sort out your problem
于 2012-05-08T08:57:58.140 回答
0

好的,有几件事:

- 您发布的代码很好。那不是问题。(NSString *) 强制转换是不必要的,但不是问题。

- 您的 NSArray 表格列表存在问题。如果您真的只将 NSMutableDictionary 添加到数组中,那么要么您做错了,要么您的数组超出了范围,当您认为您正在访问您的数组时,您正在访问该位置的内存中的其他内容。

-您如何在控制器中保留对 tablelist 的引用?

- 将您的第二个代码块更改为此,我敢打赌您会找到您的问题(这些对象之一上的保留计数 == 0):

NSLog(@"retain count=%d",[tableList retainCount]);
NSMutableDictionary *rowDict = [tableList objectAtIndex:arrayindex];
NSLog(@"retain count=%d",[rowDict retainCount]);
NSString *SelectedState = [rowDict objectForKey:@"downloadstatus"];
于 2012-05-08T15:21:47.347 回答
-2

你没有分配 NSMutableDictionary 所以 NSMutableDictionary 是空的并且你的应用程序崩溃了。

NSMutableDictionary *rowDict = [[NSMutableDictionary alloc]init];

rowDict = [tableList objectAtIndex:arrayindex];
NSString *SelectedState = [rowDict objectForKey:@"downloadstatus"];

祝你好运

于 2012-05-08T09:40:25.893 回答