UITableview NSarray isEqualToString 无法识别的选择器发送到实例
Posted
技术标签:
【中文标题】UITableview NSarray isEqualToString 无法识别的选择器发送到实例【英文标题】:UITableview NSarray isEqualToString unrecognized selector sent to instance 【发布时间】:2015-10-16 13:27:32 【问题描述】:我正在尝试在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 = "";
所以我确信它对我来说即将到来的字符串。
【问题讨论】:
很容易解决。完成解析后尝试放置断点。您将看到分配给字符串预期对象的数组。在实例化单元格之前还要检查一下。 我已经在 cell.textLabel.text=card.adi;它崩溃了。 card.adi 一定有问题 什么是阿迪?它在哪里声明?是数组还是字符串? 将您对valueForKey:
的滥用替换为对objectForKey:
的调用。
【参考方案1】:
嗯,日志说明了一切 - 您认为类型为 NSString
的对象实际上是 NSArray
。由于NSArray
不响应任何名为isEqualToString
的方法,它会崩溃!
以下是可能发生的情况:
查看您的 NKart
模型设置。虽然,您已将 adi
声明为 NSString
,但您实际上是在将 NSArray
类型转换为 NSString
。显然你得到了一个NSArray
,当你将此对象设置为标签的文本属性时,幕后会发生一些事情,询问当前字符串属性是否等于你试图设置的那个。这会导致崩溃!!!
【讨论】:
【参考方案2】:您在这里分配的是NSArray
,而不是NSString
:
NKart *card = [NKart taskWithTitle:[customerDictionary valueForKey:@"adi"]];
您需要仔细检查传入的 JSON,但它肯定是一个数组,而不是字符串。
你可能想要:
NSArray *adi = customerDictionary[@"adi"];
if ([adi count] > 0)
NKart *card = [NKart taskWithTitle:adi[0]];
...
虽然我不知道确切需要什么;只有你这样做。
【讨论】:
【参考方案3】: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];
//
【讨论】:
实际上我编辑了我的问题并添加了json。所以这就是dataDictionary的日志结果。 json 返回一次?还是json经常返回?以上是关于UITableview NSarray isEqualToString 无法识别的选择器发送到实例的主要内容,如果未能解决你的问题,请参考以下文章
为啥我不能在 uitableview 单元格中放置一个 nsarray