2015-10-16 87 views
0

我試圖在UITableview中顯示我的記錄。我可以得到json數據。問題開始於UITableViewCellUITableview NSarray isEqualToString無法識別的選擇器發送到實例

-(void)getData { 
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
    NSString *dateString; 
    dateString = [dateFormatter stringFromDate:[NSDate date]]; 
    NSDictionary *params = @{@"tarih":dateString,@"email":@"[email protected]",@"token":@"token",@"id":@"11"}; 
    NSString *url = [NSString stringWithFormat:@"http://crm.mywebsite.com/api/sync.php"]; 
    [manager POST:url parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) { 
     NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&testerror]; 
     NSLog(@"%@",dataDictionary); 
     _customerList = [NSMutableArray array]; 
     NSArray *customerArray = [dataDictionary valueForKey:@"kart"]; 
     for (NSDictionary *customerDictionary in customerArray) { 
      NKart *card = [NKart taskWithTitle:[customerDictionary valueForKey:@"adi"]]; 
      card.adres = [customerDictionary valueForKey:@"adres"]; 
      NSLog(@"%@",card.adres); 
      [self.customerList addObject:card]; 
      [self.tableView reloadData]; 
     } 

    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) { 
     NSLog(@"Error: %@",error); 
    }]; 

cellForRowAtIndexPath方法。

 NKart *card = [_customerList objectAtIndex:indexPath.row]; 
cell.textLabel.text= card.adi; 

當我試圖在tableview中顯示它們時,我遇到了這個錯誤。

[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x7fe04b8b5a00 
2015-10-16 16:08:25.503 SaphiraCrmm[6271:2023549] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x7fe04b8b5a00' 
*** First throw call stack: 
(
    0 CoreFoundation      0x000000010c511f65 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x000000010bf8bdeb objc_exception_throw + 48 
    2 CoreFoundation      0x000000010c51a58d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 
    3 CoreFoundation      0x000000010c467f7a ___forwarding___ + 970 
    4 CoreFoundation      0x000000010c467b28 _CF_forwarding_prep_0 + 120 
    5 UIKit        0x000000010cd3590f -[UITableViewLabel setText:] + 119 
    6 SaphiraCrmm       0x000000010ad24d95 -[CustomerListTableViewController tableView:cellForRowAtIndexPath:] + 997 
    7 UIKit        0x000000010ca0d6b3 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 782 
    8 UIKit        0x000000010ca0d7c8 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74 
    9 UIKit        0x000000010c9e3650 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3187 
    10 UIKit        0x000000010ca16595 -[UITableView _performWithCachedTraitCollection:] + 92 
    11 UIKit        0x000000010c9fe9ad -[UITableView layoutSubviews] + 218 
    12 UIKit        0x000000010c96f11c -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710 
    13 QuartzCore       0x00000001107c636a -[CALayer layoutSublayers] + 146 
    14 QuartzCore       0x00000001107babd0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 
    15 QuartzCore       0x00000001107baa4e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 
    16 QuartzCore       0x00000001107af1d5 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 
    17 QuartzCore       0x00000001107dc9f0 _ZN2CA11Transaction6commitEv + 508 
    18 QuartzCore       0x00000001107dd154 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92 
    19 CoreFoundation      0x000000010c43d9d7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 
    20 CoreFoundation      0x000000010c43d947 __CFRunLoopDoObservers + 391 
    21 CoreFoundation      0x000000010c43359b __CFRunLoopRun + 1147 
    22 CoreFoundation      0x000000010c432e98 CFRunLoopRunSpecific + 488 
    23 GraphicsServices     0x000000011027bad2 GSEventRunModal + 161 
    24 UIKit        0x000000010c8be676 UIApplicationMain + 171 
    25 SaphiraCrmm       0x000000010ad297af main + 111 
    26 libdyld.dylib      0x000000010e5f592d start + 1 
    27 ???         0x0000000000000001 0x0 + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

阿迪不是NSArray的

還有就是JSON

{ 
       adi = "TestFirm"; 
       adi2 = ""; 
       adi3 = ""; 
       adres = "\U0130STANBUL"; 
       email = ""; 
       email2 = ""; 
       facebook = ""; 
       fax = ""; 
       "fk_agent" = 2; 
       "fk_faaliyet_alani" = 0; 
       "fk_grubu" = 7; 
       "fk_il" = 34; 
       "fk_kaydi_yapan" = 0; 
       "fk_kaynagi" = 0; 
       id = 407; 
       "kayit_tipi" = 10; 
       ktar = "2014-10-30 16:28:00"; 
       linkedin = ""; 
       skype = ""; 
       tel1 = ""; 
       tel2 = ""; 
       twitter = ""; 
       vd = ""; 
       vn = ""; 
       www = ""; 
} 

所以我'確保其未來的字符串給我。

+1

這很容易解決。嘗試在完成解析後添加斷點。你會看到一個數組賦予一個字符串期望對象。在你實例化你的單元之前再檢查一下。 – erenkabakci

+0

我已經在 中做過了cell.textLabel.text = card.adi; 其崩潰。 –

+0

有些東西一定是錯的card.adi –

回答

0

您分配一個NSArray,而不是NSString,在這裏:

NKart *card = [NKart taskWithTitle:[customerDictionary valueForKey:@"adi"]]; 

你需要仔細檢查傳入的JSON,但它肯定是一個數組,而不是字符串。

你可能想:

NSArray *adi = customerDictionary[@"adi"]; 
if ([adi count] > 0) { 
    NKart *card = [NKart taskWithTitle:adi[0]]; 
    ... 
} 

雖然我不知道究竟什麼是必需的;只有你做到。

1

那麼,日誌說這一切 - 你認爲是NSString類型的對象實際上是NSArray。由於NSArray不響應任何稱爲isEqualToString的方法,因此崩潰!

以下是可能發生的:

查看您所在NKart模型設置。雖然,您宣稱adiNSString,但您實際上正在鑄造NSArrayNSString。顯然你得到了一個NSArray,當你將這個對象設置爲你的標籤的文本屬性時,幕後會出現一些問題,詢問當前的字符串屬性是否與你正在設置的字符串屬性相同。這導致崩潰!

0

isEqualToString方法仍然在NSString類。但此方法不存在NSArray類。

提供的

NSLog(@"%@",dataDictionary); 

值迅速解決您的問題。

第二次嘗試:你的代碼仍然是一些不必要的代碼。

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&testerror]; 
NSLog(@"%@",dataDictionary); 
_customerList = [NSMutableArray array]; 
//NSArray *customerArray = [_customerList addObject:[dataDictionary valueForKey:@"kart"]]; 
//for (NSDictionary *customerDictionary in customerArray) { 
    NKart *card = [NKart taskWithTitle:[customerDictionary valueForKey:@"adi"]]; 
    card.adres = [customerDictionary valueForKey:@"adres"]; 
    NSLog(@"%@",card.adres); 
    [self.customerList addObject:card]; 
    [self.tableView reloadData]; 
//} 
+0

其實我編輯我的問題,並添加了json.So這是dataDictionary的日誌結果。 json返回一次? –

+0

或json返回頻繁? – Jamil

相關問題