【发布时间】:2014-05-23 13:12:04
【问题描述】:
先生的问题是-----我正在从 url 和 arrayobj 中获取数据。当我运行这个项目时,我得到了这个错误。我做错了什么,项目中的语句或代码请检查connectiondidfinishloading和cellforrowanindexpath。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error ;
NSMutableDictionary *dictobj = [NSJSONSerialization JSONObjectWithData:dataobj options:NSJSONReadingMutableContainers error:&error];
newsobj=[dictobj objectForKey:@"news"];
//NSArray *longdeceobj=[newsobj objectForKey:@"long_desc"];
for (NSDictionary *dict in newsobj)
{
NSDictionary *title=[dict objectForKey:@"title"];
[arrayobj addObject:title];
}
[[self tableobj]reloadData];
}
//这是索引路径代码处的行单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString*cellide=@"identifier";
newcustomobj=(newsCustamView *) [tableView dequeueReusableCellWithIdentifier:cellide];
if (newcustomobj==nil)
{
[[NSBundle mainBundle]loadNibNamed:@"newsCustamView" owner:self options:nil];
}
newcustomobj.newstitle.text=[[[arrayobj objectAtIndex:0]objectForKey:@"news"]objectForKey:@"title"];
return newcustomobj;
}
错误是:
2014-05-23 18:34:04.913 newsdata[6518:90b] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x9c515c0
2014-05-23 18:34:04.915 newsdata[6518:90b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x9c515c0'
*** First throw call stack:
(
0 CoreFoundation 0x01b851e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x015cb8e5 objc_exception_throw + 44
2 CoreFoundation 0x01c22243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x01b7550b ___forwarding___ + 1019
4 CoreFoundation 0x01b750ee _CF_forwarding_prep_0 + 14
5 newsdata 0x00008a13 -[ViewController tableView:cellForRowAtIndexPath:] + 435
6 UIKit 0x0038411f -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412
7 UIKit 0x003841f3 -[UITableView _createPreparedCellForGlobalRow:] + 69
8 UIKit 0x00365ece -[UITableView _updateVisibleCellsNow:] + 2428
9 UIKit 0x0037a6a5 -[UITableView layoutSubviews] + 213
10 UIKit 0x002fa964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
11 libobjc.A.dylib 0x015dd82b -[NSObject performSelector:withObject:] + 70
12 QuartzCore 0x03dcd45a -[CALayer layoutSublayers] + 148
13 QuartzCore 0x03dc1244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
14 QuartzCore 0x03dc10b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
15 QuartzCore 0x03d277fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
16 QuartzCore 0x03d28b85 _ZN2CA11Transaction6commitEv + 393
17 QuartzCore 0x03d29258 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
18 CoreFoundation 0x01b4d36e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
19 CoreFoundation 0x01b4d2bf __CFRunLoopDoObservers + 399
20 CoreFoundation 0x01b2b254 __CFRunLoopRun + 1076
21 CoreFoundation 0x01b2a9d3 CFRunLoopRunSpecific + 467
22 CoreFoundation 0x01b2a7eb CFRunLoopRunInMode + 123
23 GraphicsServices 0x03c8f5ee GSEventRunModal + 192
24 GraphicsServices 0x03c8f42b GSEventRun + 104
25 UIKit 0x0028bf9b UIApplicationMain + 1225
26 newsdata 0x000119cd main + 141
27 libdyld.dylib 0x02330725 start + 0
28 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
【问题讨论】:
-
我的猜测是在不知道 JSON 的情况下(您在问题中出现):
[[[arrayobj objectAtIndex:0]objectForKey:@"news"]objectForKey:@"title"];何时调用objectForKey(第一个或第二个,因为我们不知道 JSON) ,实际上您的对象是NSString而不是NSDictionary -
我认为您必须在此处粘贴您的 JSON 格式,以便其他人可以帮助您纠正错误。
-
首先感谢您的回复,我在 json 格式中做错了。但是我学习并阅读了一些 json 主题,一位朋友建议我这样做可以轻松解决您的问题...
标签: ios objective-c nsarray