2017-07-18 109 views
0

我使用AFNetworking 3在json中下載了我的Firebase數據庫。一切正常,但函數cellForRoAtIndexPath出現崩潰。 由於下載JSON異步崩潰

工作:

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陣列= [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年7月18日10:00:26.787 jojol67 [7003:4420828] ***終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因: ' - [__ NSDictionaryI objectAtIndex:]:無法識別的選擇發送到實例0x61000086c8c0' ***第一擲調用堆棧: ( 0的CoreFoundation 0x000000010c084b0b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010e5c9141 objc_exception_throw + 48 2 CoreFoundation 0x000000010c0f4134 - [NSObje CT(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:通知:] + 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主+ 111 24 libdyld。dylib 0x000000010f31d65d start + 1 25 ??? 0x0000000000000001爲0x0 + 1 ) 的libC++ abi.dylib:與類型的未捕獲的異常終止NSException

///////火力地堡實時數據庫////////

enter image description here

+0

崩潰=>你有一個崩潰日誌呢? – Larme

+0

錯誤消息是顯式的,並用JSON結構確認:您的JSON是頂級NSDictionary,而不是Array。 – Larme

+0

你的self.contestArray是NSDictionary不是NSArray所以你必須訪問NSDictionary * array = [self.contestArray objectForKey:「key」];使用NSLog(@「%@」,self.contestArray)來查看self.contestArray的鍵和值 –

回答

0

它會幫助你對這種性反應:

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 
} 
+0

@victorbill試試這個會狡猾地解決你的問題。 –

+0

非常感謝,它的工作:) –

3

您的id responseObject不是數組,因爲您的JSON包含的對象不是數組。應用程序崩潰,因爲您在NSDictionary對象上調用objectAtIndex

如果要訪問此JSON作爲一個數組,你可能需要將其格式化爲這樣的事情,

[{ 
    "Concours": 1, 
    "Description": "Description du concours", 
    "Title": "Titre" 
}, { 
    "Concours": 2, 
    "Description": "Description du concours", 
    "Titre": "iPhone 6" 
}] 
+0

那麼我們該怎麼做?謝謝 –

+0

@victorbill我已經添加了更多信息。 – sleepwalkerfx

+0

@sleepwalkerfx「Concours-1」和「Concours-2」是分開的對象,不是用於內部對象的值...這些是主要對象 –

0

當前JSON:

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

應該如何:

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

「 - [NSDictionaryI objectAtIndex:]:無法識別的選擇器發送到實例0x61000086c 8c0「 看來,self.contestArrayNSDictionary類;

您可以登錄responseObject它可能是一個Dictionary,而不是Array;