我正试图在UITableview
中显示我的记录。我可以得到json数据。问题出现在UITableViewCell
。
-(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@email.com",@"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
Adi不是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 = "";
}
所以我确定它即将到来。
答案 0 :(得分:1)
好吧,日志说明了一切 - 您认为属于NSString
类型的对象实际上是NSArray
。由于NSArray
没有响应任何名为isEqualToString
的方法,因此崩溃了!
以下是可能发生的事情:
查看您的NKart
型号设置。虽然您已将adi
声明为NSString
,但您实际上是将NSArray
输入到NSString
。显然你得到一个NSArray
,当你将这个对象设置为你的标签的文本属性时,幕后会发生一些事情,询问当前的字符串属性是否等于你正在尝试的属性设置。这导致崩溃!!!
答案 1 :(得分: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]];
...
}
虽然我不知道完全需要什么;只有你这样做。
答案 2 :(得分: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];
//}