我从API下载JSON响应并将其转换为NSArray(“theArray”)。
JSON格式如下所示
[{"RequestID":"1", "Title":"My Title"},{"RequestID":"1", "Title":"My Title"}]
转换发生在ViewDidLoad
中- (void)viewDidLoad {
[super viewDidLoad];
//JSON Request
//Prepare the link
NSURL * url = [[NSURL alloc] initWithString:@"getCategory.php"];
//prepare the request object
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
[urlRequest setHTTPMethod:@"POST"];
NSString *poststring = @"username=X";
[urlRequest setHTTPBody:[poststring dataUsingEncoding:NSUTF8StringEncoding]];
//prepare the variables for the json request
NSData *urlData;
NSURLResponse *response;
NSError *error;
//make the syncronous request
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
//construct an array arrount the data from the response
NSMutableArray *object = [NSJSONSerialization JSONObjectWithData:urlData options:0 error:&error];
theArray = [[NSMutableArray alloc] init];
theArray = object;
}
但是当调用以下方法来填充表视图的单元格时,我得到一个NSExceptions错误
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSMutableArray *subArray = [theArray objectAtIndex:indexPath.row];
cell.textLabel.text = [subArray objectAtIndex:1];
return cell;
}
起初我认为问题与数组中的无效指针有关,因此我将其分解为“subArray”,然后在此数组的索引1处引用该对象以获取值
错误如下:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x79fed110'
*** First throw call stack:
(
0 CoreFoundation 0x00af9a84 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x005bae02 objc_exception_throw + 50
2 CoreFoundation 0x00b02dd3 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x00a40cdd ___forwarding___ + 1037
4 CoreFoundation 0x00a408ae _CF_forwarding_prep_0 + 14
5 Document Collection 2 0x000e7ae0 -[ViewRequest tableView:cellForRowAtIndexPath:] + 288
6 UIKit 0x0100ea19 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 782
7 UIKit 0x0100eb47 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 90
8 UIKit 0x00fdd7f1 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3317
9 UIKit 0x00ffdcdd __29-[UITableView layoutSubviews]_block_invoke + 52
10 UIKit 0x010188f3 -[UITableView _performWithCachedTraitCollection:] + 88
11 UIKit 0x00ffdbb2 -[UITableView layoutSubviews] + 214
12 UIKit 0x00f52eb7 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 813
13 libobjc.A.dylib 0x005cf059 -[NSObject performSelector:withObject:] + 70
14 QuartzCore 0x048ee80a -[CALayer layoutSublayers] + 144
15 QuartzCore 0x048e24ee _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 388
16 QuartzCore 0x048e2352 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
17 QuartzCore 0x048d4e8b _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 317
18 QuartzCore 0x04908e03 _ZN2CA11Transaction6commitEv + 561
19 QuartzCore 0x0490a674 _ZN2CA11Transaction17flush_transactionEv + 50
20 UIKit 0x00eb9c8a _afterCACommitHandler + 197
21 CoreFoundation 0x00a1361e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
22 CoreFoundation 0x00a1357e __CFRunLoopDoObservers + 398
23 CoreFoundation 0x00a08efc __CFRunLoopRun + 1340
24 CoreFoundation 0x00a08706 CFRunLoopRunSpecific + 470
25 CoreFoundation 0x00a0851b CFRunLoopRunInMode + 123
26 GraphicsServices 0x04188664 GSEventRunModal + 192
27 GraphicsServices 0x041884a1 GSEventRun + 104
28 UIKit 0x00e891eb UIApplicationMain + 160
29 Document Collection 2 0x000e91aa main + 138
30 libdyld.dylib 0x03155a21 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
答案 0 :(得分:1)
subArray
不是NSArray
,而是NSDictionary
。
试试这个:
NSDictionary *dict = theArray[indexPath.row];
cell.textLabel.text = dict[@"Title"];
编辑: 如果您正在使用它,请删除以下行:
theArray = [[NSMutableArray alloc] init];
您在下一行设置它,设置两次没有意义。