0

I download my Firebase database in json with AFNetworking 3. Everything works fine but there is a crash in the function cellForRoAtIndexPath. Thanks

Work :

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); }]; 

Work :

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return [self.contestArray count]; } 

Not work : (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" } } 

Log Crash :

-[NSDictionaryI objectAtIndex:]: unrecognized selector sent to instance 0x61000086c8c0 2017-07-18 10:00:26.787 jojol67[7003:4420828] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI objectAtIndex:]: unrecognized selector sent to instance 0x61000086c8c0' *** First throw call stack: ( 0 CoreFoundation 0x000000010c084b0b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010e5c9141 objc_exception_throw + 48 2 CoreFoundation 0x000000010c0f4134 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 3 CoreFoundation 0x000000010c00b840 ___forwarding_ + 1024 4 CoreFoundation 0x000000010c00b3b8 _CF_forwarding_prep_0 + 120 5 jojol67 0x000000010756db3f -[DEMOConcoursTableViewController collectionView: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 QuartzCore 0x000000010c698526 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 370 13 QuartzCore 0x000000010c6983a0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 14 QuartzCore 0x000000010c627e92 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294 15 QuartzCore 0x000000010c654130 _ZN2CA11Transaction6commitEv + 468 16 QuartzCore 0x000000010c654b37 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 115 17 CoreFoundation 0x000000010c02a717 CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23 18 CoreFoundation 0x000000010c02a687 __CFRunLoopDoObservers + 391 19 CoreFoundation 0x000000010c00f720 __CFRunLoopRun + 1200 20 CoreFoundation 0x000000010c00f016 CFRunLoopRunSpecific + 406 21 GraphicsServices 0x00000001103c2a24 GSEventRunModal + 62 22 UIKit 0x000000010c830134 UIApplicationMain + 159 23 jojol67 0x000000010752ef5f main + 111 24 libdyld.dylib 0x000000010f31d65d start + 1 25 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

/////// Firebase realtime database ////////

enter image description here

6
  • A crash => You have a crash log then? Commented Jul 18, 2017 at 7:58
  • The error message is explicit and confirmed with the JSON structure: your JSON is a NSDictionary at top level, not an Array. Commented Jul 18, 2017 at 8:02
  • your self.contestArray is NSDictionary not NSArray so you must access with NSDictionary *array = [self.contestArray objectForKey:"key"]; use NSLog(@"%@",self.contestArray) to see the keys and values of self.contestArray Commented Jul 18, 2017 at 8:03
  • @ReinierMelian //////// work //////// NSDictionary *array = [self.contestArray valueForKey:@"Concours-1"]; /////// not work (I need that one) //////// NSDictionary *array2 = [self.contestArray objectAtIndex:indexPath.row]; Commented Jul 18, 2017 at 8:07
  • Indeed your retrieved dictionary don't have any array inside Commented Jul 18, 2017 at 8:09

4 Answers 4

3

Your id responseObject is not an array because your JSON contains an object not an array. App crashes because you are calling objectAtIndex on a NSDictionary object.

If you want to access this JSON as an array, you may want to format it to something like this,

[{ "Concours": 1, "Description": "Description du concours", "Title": "Titre" }, { "Concours": 2, "Description": "Description du concours", "Titre": "iPhone 6" }] 
Sign up to request clarification or add additional context in comments.

5 Comments

So how do we do that? Thanks
@victorbill I've added more info.
@sleepwalkerfx "Concours-1" and "Concours-2" are separated Object , not for the internal objects value... these are the main object
@victorbill check my answer it will help you 100% without any change in responce json. happy coding :)
@ParvendraSingh I could've provided the same solution, but it's an extremely bad practice and a design, which will lead to more difficulties in future. Since he's using a data source for a collection view right way to do is changing the JSON schema to an array format while it's still possible, rather than ending up with more critical problems later. Dictionaries are not ordered data structures , so that is another pitfall he has to deal with already with that code.
0

It will help you for such responce:

NSDictionary *jsonDict =responseObject; NSArray *allKeys = [jsonDict allKeys]; NSMutableArray *allDataArray = [NSMutableArray new]; for (int x = 0; x<allKeys.count; x++) { NSString *key = [allKeys objectAtIndex:x]; [allDataArray addObject:[jsonDict valueForKey:key]]; // then reload your collection on allDataArray } 

1 Comment

@victorbill Try this it will surly solve your problem.
0

Current JSON:

{ "Concours-1" : { "Description" : "Description du concours", "Title" : "Titre" }, "Concours-2" : { "Description" : "Description du concours", "Titre" : "iPhone 6" } } 

How it should be:

[ { "Description": "Description du concours", "Title": "Titre" }, { "Description": "Description du concours", "Titre": "iPhone 6" } ] 

Comments

0

"-[NSDictionaryI objectAtIndex:]: unrecognized selector sent to instance 0x61000086c8c0 " It seems that the self.contestArray is NSDictionary class;

You can log the responseObject it maybe a Dictionary , not Array;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.