NS阵列的NSarray

时间:2012-05-08 07:21:48

标签: iphone nsarray

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

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

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

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

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

这是我的consol上的崩溃显示

 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'

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

好的,有几件事:

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

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

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

- 更改你的第二个代码块,我打赌你发现你的问题(retainCount在其中一个对象上== 0):

NSLog(@"retain count=%d",[tableList retainCount]);
NSMutableDictionary *rowDict = [tableList objectAtIndex:arrayindex];
NSLog(@"retain count=%d",[rowDict retainCount]);
NSString *SelectedState = [rowDict objectForKey:@"downloadstatus"];

答案 2 :(得分:-2)

您没有分配NSMutableDictionary,因此NSMutableDictionary为空,您的应用程序崩溃。

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

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