数组错误:以NSException类型的未捕获异常终止

时间:2014-05-27 18:54:37

标签: ios objective-c viewcontroller

我正在尝试通过查看orignal应用程序的源代码并学习它然后尝试重新键入单独的文件(并确保Interface Builder中的各个部分与原始文件匹配)来重新创建基本应用程序。我遇到的一个问题是我收到以下错误:

2014-05-27 14:02:10.701 Nav Ctrl Practice[3513:60b] adding stock price:  to company at index 1
2014-05-27 14:02:10.703 Nav Ctrl Practice[3513:60b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000101a3d495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010179c99e objc_exception_throw + 43
    2   CoreFoundation                      0x00000001019e3745 -[__NSArrayM objectAtIndex:] + 213
    3   Nav Ctrl Practice                   0x0000000100003ad2 -[ParentViewController connectionDidFinishLoading:] + 530
    4   Foundation                          0x000000010148d36b __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke + 48
    5   Foundation                          0x0000000101340bdb -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 210
    6   Foundation                          0x0000000101340aec -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 69
    7   CFNetwork                           0x0000000106950637 ___ZN27URLConnectionClient_Classic26_delegate_didFinishLoadingEU13block_pointerFvvE_block_invoke + 107
    8   CFNetwork                           0x000000010694e802 ___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 + 84
    9   CoreFoundation                      0x00000001019e3f74 CFArrayApplyFunction + 68
    10  CFNetwork                           0x00000001068c13e7 _ZN19RunloopBlockContext7performEv + 133
    11  CFNetwork                           0x00000001068c1217 _ZN17MultiplexerSource7performEv + 247
    12  CFNetwork                           0x00000001068c103a _ZN17MultiplexerSource8_performEPv + 72
    13  CoreFoundation                      0x00000001019ccd21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    14  CoreFoundation                      0x00000001019cc5f2 __CFRunLoopDoSources0 + 242
    15  CoreFoundation                      0x00000001019e846f __CFRunLoopRun + 767
    16  CoreFoundation                      0x00000001019e7d83 CFRunLoopRunSpecific + 467
    17  GraphicsServices                    0x0000000103bb4f04 GSEventRunModal + 161
    18  UIKit                               0x0000000100349e33 UIApplicationMain + 1010
    19  Nav Ctrl Practice                   0x0000000100005eb3 main + 115
    20  libdyld.dylib                       0x0000000101fc25fd start + 1
    21  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我在这个网站上发现了一些与上面显示的错误相关的链接(“以NSException类型的未捕获异常终止”),但是没有一个具体原因似乎与我的匹配。我正在使用的代码完全从工作文件中复制。所以我不确定我在这里缺少什么。我已经在该部分的源代码下方包含了数组的问题。

//
//  ParentViewController.m
//

#import "ParentViewController.h"
#import "ChildViewController.h"
#import "DataAccess.h"
#import "sqlite3.h"



@interface ParentViewController ()
{

    NSMutableArray *arrayOfProducts;
    NSMutableArray *arrayOfCompanies;
    sqlite3 *productDB;
    sqlite3 *companyDB;

}


@end

@implementation ParentViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.



    arrayOfProducts = [[NSMutableArray alloc] init];

    [[self myTableView]setDelegate:self];
    [[self myTableView]setDataSource:self];


    self.dataAccess = [[DataAccess alloc] init];
    [self.dataAccess setCompanyListFromDB];



    self.childVC = [[ChildViewController alloc] init];


     NSMutableString *urlQuotes = [NSMutableString stringWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s="];


    for(int i =0; i<self.dataAccess.companyList.count; i++) {
        Company *company = self.dataAccess.companyList[i];
        [urlQuotes appendFormat:@"%@", company.stockSymbol];

        if(i<self.dataAccess.companyList.count-1)
    {       [urlQuotes appendString:@","];
    }
        [urlQuotes appendString:@"&f=l1,"];

        NSLog(@"Quote URL: %@", urlQuotes);


        _responseData =[[NSMutableData alloc] init];
        NSURLRequest *request = [NSURLRequest requestWithURL:
                                 [NSURL URLWithString:urlQuotes]] ; //we are creating the request here
        NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; //we are creating the url connection and firing the request here
        [conn start]; //start the connection



    }

}



#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{


    NSLog(@"numberOfSectionsInTableView");
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberOfRowsInSection");



    return [self.dataAccess.companyList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{



    static NSString *CellIdentifier = @"Cell";

           UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"];
    }

    int row = indexPath.row;
    NSLog(@"Cell at index: %d", row);


    //Find the company for this row in the table
    Company * company = self.dataAccess.companyList[row];
    NSLog(@"Got company %@ at index: %d from companyList Array", company.name, row);

    // Configure the cell...

    //make the row's text be the company's name
    cell.textLabel.text = company.name;
    [[cell imageView] setImage: [UIImage imageNamed: company.logo]];
    cell.detailTextLabel.text = company.stockPrice;


    return cell;
}



#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    int row = indexPath.row;
    NSLog(@"Cell at index: %d", row);
    self.childVC.company = self.dataAccess.companyList[row];
    //  [self.dataAccess test];
    self.childVC.dataAccess2 = self.dataAccess;
    [self.navigationController pushViewController:self.childVC animated:YES];
}




#pragma mark NSURLConnection Delegate Methods

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to the instance variable you declared
    [_responseData appendData:data];
}



- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
     NSString *str =  [[NSString alloc] initWithData:_responseData encoding:NSUTF8StringEncoding];
    NSMutableArray *responseArray= [NSMutableArray arrayWithArray:   [str componentsSeparatedByString:@"\n"]  ];

    //includes extra blank character at the end; just leaving it there for now
    NSLog(@"Response Array: %@", responseArray);

    for(int i=0;i<[self.dataAccess.companyList count];i++){
        Company *company = self.dataAccess.companyList[i];
        NSMutableString *string = responseArray[i];
        NSLog(@"adding stock price: %@ to company at index %d", string, i);
        company.stockPrice = string;

    } //simple loop for referring to items in companyList


    [self.tableView reloadData];


    NSLog (@"connectionDidFinishLoading Done");


}


-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog (@"connection didFailWithError: %@", error.debugDescription);



}



@end

1 个答案:

答案 0 :(得分:2)

问题似乎出现在connectionDidFinishLoading:

的实施中
for(int i=0;i<[self.dataAccess.companyList count];i++){
    Company *company = self.dataAccess.companyList[i];
    NSMutableString *string = responseArray[i];
    ...
}

在这里,您对icompanyList使用相同的迭代器变量(responseArray),即使它们的大小不同。您应该确保i小于responseArraycompanyList的长度:

for(int i=0;i<[self.dataAccess.companyList count] && i<[responseArray count];i++){
    Company *company = self.dataAccess.companyList[i];
    NSMutableString *string = responseArray[i];
    ...
}