【发布时间】:2017-12-22 23:06:48
【问题描述】:
我使用 AFNetworking 3 以 json 格式下载了我的 Firebase 数据库。一切正常,但函数 cellForRoAtIndexPath 发生了崩溃。 谢谢
工作:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:@"https://jojol-concours-lists.firebaseio.com/.json" parameters:URLParameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
self.contestArray = responseObject;
[_collectionView reloadData];
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
工作:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.contestArray count];
}
不起作用:(NSDictionary *array = [self.contestArray objectAtIndex:indexPath.row];)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
DemoCollectionViewCell *retVal = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewCell"
forIndexPath:indexPath];
//////// Not work ///////
NSDictionary *array = [self.contestArray objectAtIndex:indexPath.row];
//////// Not work ///////
retVal.name.text = @"";
retVal.contentView.layer.cornerRadius = 10;
retVal.contentView.layer.masksToBounds = YES;
return retVal;
}
JSON:
{
"Concours-1" : {
"Description" : "Description du concours",
"Title" : "Titre"
},
"Concours-2" : {
"Description" : "Description du concours",
"Titre" : "iPhone 6"
}
}
日志崩溃:
-[NSDictionaryI objectAtIndex:]:无法识别的选择器发送到实例 0x61000086c8c0 2017-07-18 10:00:26.787 jojol67[7003:4420828] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSDictionaryI objectAtIndex:]:无法识别的选择器发送到实例 0x61000086c8c0” *** 首先抛出调用堆栈: ( 0 核心基础 0x000000010c084b0b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010e5c9141 objc_exception_throw + 48 2核心基础0x000000010c0f4134-[NSObject(NSObject)不识别选择器:]+132 3 核心基础 0x000000010c00b840 ___forwarding_ + 1024 4 核心基础 0x000000010c00b3b8 _CF_forwarding_prep_0 + 120 5 jojol67 0x000000010756db3f -[DEMOConcoursTableViewController 集合视图:cellForItemAtIndexPath:] + 191 6 UIKit 0x000000010d162925-[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 446 7 UIKit 0x000000010d162761-[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 35 8 UIKit 0x000000010d1679bd-[UICollectionView _updateVisibleCellsNow:] + 4764 9 UIKit 0x000000010d16d38e-[UICollectionView layoutSubviews] + 313 10 UIKit 0x000000010c8f355b-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1268 11 QuartzCore 0x000000010c6a4904 -[CALayer layoutSublayers] + 146 12 石英核心 0x000000010c698526 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 370 13 石英核心 0x000000010c6983a0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 14 石英核心 0x000000010c627e92 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294 15 石英核心 0x000000010c654130 _ZN2CA11Transaction6commitEv + 468 16 石英核心 0x000000010c654b37 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 115 17 核心基础 0x000000010c02a717 CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23 18 核心基础 0x000000010c02a687 __CFRunLoopDoObservers + 391 19 核心基础 0x000000010c00f720 __CFRunLoopRun + 1200 20 核心基础 0x000000010c00f016 CFRunLoopRunSpecific + 406 21 图形服务 0x00000001103c2a24 GSEventRunModal + 62 22 UIKit 0x000000010c830134 UIApplicationMain + 159 23 jojol67 0x000000010752ef5f 主要 + 111 24 libdyld.dylib 0x000000010f31d65d 开始 + 1 25 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止
/////// Firebase 实时数据库 ////////
【问题讨论】:
-
崩溃 => 那么您有崩溃日志吗?
-
错误消息是明确的,并通过 JSON 结构确认:您的 JSON 是顶级的 NSDictionary,而不是数组。
-
你的 self.contestArray 是 NSDictionary 而不是 NSArray 所以你必须使用 NSDictionary *array = [self.contestArray objectForKey:"key"];使用 NSLog(@"%@",self.contestArray) 查看 self.contestArray 的键和值
-
@ReinierMelian //////// 工作 //////// NSDictionary *array = [self.contestArray valueForKey:@"Concours-1"]; /////// 不起作用(我需要那个) //////// NSDictionary *array2 = [self.contestArray objectAtIndex:indexPath.row];
-
确实你检索到的字典里面没有任何数组
标签: ios objective-c json crash