【发布时间】:2015-02-02 06:01:53
【问题描述】:
这是我一周前开始学习为 iOS 开发以来的一个新错误,我完全不知道是什么原因导致它以及为什么应用程序突然崩溃。如果您可以查看我的代码并建议如何修复此运行时错误,那么我的代码一定有问题。
*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (1)'
*** First throw call stack:
(0x2514c49f 0x32902c8b 0x2514c3e5 0x25079a6d 0xd01cb 0x2863f9fb 0x2863f9a1 0x2862a613 0x28747781 0x287fdc6b 0x286041ad 0x286390c1 0x2863899d 0x2860f15d 0x28882ab9 0x2860dbb9 0x25112d57 0x25112167 0x251107cd 0x2505e3c1 0x2505e1d3 0x2c45c0a9 0x2866dfa1 0xa3911 0x32e82aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
这是我的代码 sn-p
- (IBAction)changeIns:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
if (selectedSegment == 0) {
//toggle the correct view to be visible
NSLog(@"Clicked");
jsonData = [tArray objectAtIndex:0];
self.displayInsurer.text=jsonData[@"insurerName"];
}
else if (selectedSegment == 1) {
//toggle the correct view to be visible
jsonData = [tArray objectAtIndex:1];
self.displayInsurer.text=jsonData[@"insurerName"];
}
else if (selectedSegment == 2) {
//toggle the correct view to be visible
}
else if (selectedSegment == 3) {
//toggle the correct view to be visible
}
else if (selectedSegment == 4) {
//toggle the correct view to be visible
}
else if (selectedSegment == 5) {
//toggle the correct view to be visible
}
else if (selectedSegment == 6) {
//toggle the correct view to be visible
}
else if (selectedSegment == 7) {
//toggle the correct view to be visible
}
else if (selectedSegment == 8) {
//toggle the correct view to be visible
}
}
-(void)setVariableFromNetwork
{
// NSInteger success = 0;
@try {
NSString *post =[[NSString alloc] initWithFormat:@"uid=%@&tag=getVehicleInfo",[[NSUserDefaults standardUserDefaults] stringForKey:@"userID"]];
NSLog(@"PostData: %@",post);
NSURL *url=[NSURL URLWithString:@"URL"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response code: %ld", (long)[response statusCode]);
if ([response statusCode] >= 200 && [response statusCode] < 300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
NSError *error = nil;
tArray = [NSJSONSerialization JSONObjectWithData:urlData options:kNilOptions error:&error];
NSLog(@"objects %@", [tArray description]);
NSLog(@"first object %@", [tArray objectAtIndex:0]);
NSDictionary *jsonData = [tArray objectAtIndex:0];
NSLog(@"dictionary %@", [jsonData description]);
NSLog(@"vehicle %@", jsonData[@"vehicleMake"]);
NSLog(@"Vehicle Number %@", jsonData[@"vehicleRegNo"]);
int count = [tArray count];
NSMutableArray *mySegments = [[NSMutableArray alloc] initWithCapacity:count];
for (int i = 0; i<count; i++) {
jsonData = [tArray objectAtIndex:i];
//NSString * desc = jsonData[@"vehicleMake"],"-", jsonData[@"vehicleMake"];
[mySegments addObject:jsonData[@"vehicleMake"]];
[mySegments addObject:jsonData[@"vehicleRegNo"]];
}
[self.carSegment removeAllSegments];
self.carSegment = [self.carSegment initWithItems:mySegments];
}
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
[self alertStatus:@"Sign in Failed." :@"Error!" :0];
}
}
【问题讨论】:
-
Objective-C 中的异常仅用于不可恢复的错误,而不是控制流。您需要编写不会导致异常的代码。
-
为了强调 Zaph 的观点,Objective-C 的 try-catch 块不会捕捉到您在开发期间通过适当的测试和调试无法阻止的东西。
-
原因是“Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (1)”
标签: ios objective-c json