iOS   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – 超出空阵列崩溃范围的索引’5′大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我正在构建一个RSS-Reader并在导航栏的右上角放置一个刷新按钮.它工作正常,我没有崩溃.但是,如果我在滚动期间按下刷新按钮,应用程序崩溃.我不知道问题出在哪里.我分析了这个项目,但@R_772_9112@东西…… 所以这是我得到的错误: 2012-01-22 16:36:48.205 GYSA[712:707] *** TerminaTing app due to uncaught exception '
我正在构建一个RSS-Reader并在导航栏的右上角放置一个刷新按钮.它工作正常,我没有崩溃.但是,如果我在滚动期间按下刷新按钮,应用程序崩溃.我不知道问题出在哪里.我分析了这个项目,但@R_772_9112@东西……

所以这是我得到的错误

2012-01-22 16:36:48.205 GYSA[712:707] *** TerminaTing app due to uncaught exception 'NSRangeException',reason: '*** -[__NSArraym objectATindex:]: index 5 beyond bounds for empty array'
*** First throw call stack:
(0x37adb8bf 0x315c11e5 0x37a24b6b 0x7913 0x34ef39cb 0x34ef2aa9 0x34ef2233 0x34e96d4b 0x37a3a22b 0x33231381 0x33230f99 0x3323511b 0x33234e57 0x3325c6f1 0x3327f4c5 0x3327f379 0x37249f93 0x3747b891 0x37aa4f43 0x37aaf553 0x37aaf4f5 0x37aae343 0x37a314dd 0x37a313a5 0x375affcd 0x34ec1743 0x2ac9 0x2a54)
terminate called throwing an exception(gdb)

这是我的代码

#import "RSSFunViewController.h"
#import "BlogRSSParser.h"
#import "BlogRSS.h"

@implementation RSSFunViewController

@synthesize RSSParser = _RSSParser;
@synthesize tableView = _tableView;
@synthesize appDelegate = _appDelegate;
@synthesize toolbar = _toolbar;

-(void)toolbarInit{
    UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc]
                                   initWithBarButtonSystemItem:UIBarButtonSystemItemrefresh
                                   target:self action:@SELEctor(reloadRSS)];
    refreshButton.enabled = YES;
    self.navigationItem.rightBarButtonItem = refreshButton;
    [refreshButton release];
    UIImage *image = [UIImage imagenamed: @"navigationbar.png"];
    UIImageView *imageview = [[UIImageView alloc] initWithImage: image];

    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView: imageview];
    self.navigationItem.leftBarButtonItem = button;
    [imageview release];
    [button release];
}


// Implement viewDidLoad to do additional setup after loading the view,typically from a nib.
- (void)viewDidLoad {

    [super viewDidLoad];
    self.view.autoresizesSubviews = YES;
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self toolbarInit];
    _RSSParser = [[BlogRSSParser alloc]init];
    self.RSSParser.delegate = self;
    [[self RSSParser]startProcess];
}

-(void)reloadRSS{
    [self toggleToolBarButtons:NO];
    [[self RSSParser]startProcess];
}

-(void)toggleToolBarButtons:(BOOL)newState{
    NSArray *toolbarItems = self.toolbar.items;
    for (UIBarButtonItem *item in toolbarItems){
        item.enabled = newState;
    }   
}

//Delegate method for blog parser will get fired when the process is completed
- (void)processCompleted{
    //reload the table view
    [self toggleToolBarButtons:YES];
    [[self tableView]reloadData];
}

-(void)processHasErrors{
    //Might be due to Internet
    UIAlertView *alert = [[UIAlertView alloc] initWithtitle:@"Achtung!" message:@"LEIDer ist es im Moment nicht möglich eine Verbindung zum Internet herzustellen. OhnE internetverbindung ist die App nur in beschränktem Umfang nutzbar!"
                                                   delegate:nil cancelButtontitle:@"OK" otherButtontitles: nil];
    [alert show];   
    [alert release];
    [self toggleToolBarButtons:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[[self RSSParser]RSSItems]count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RSSItemCell"];
    if(nil == cell){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reusEIDentifier:@"RSSItemCell"]autorelease];
    }
    cell.textLabel.text = [[[[self RSSParser]RSSItems]objectATindex:indexPath.row]title];
    cell.detailTextLabel.text = [[[[self RSSParser]RSSItems]objectATindex:indexPath.row]description];
    cell.accessoryType = UITableViewCellAccessoryDisclosureInDicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSELEctRowATindexPath:(NSIndexPath *)indexPath {
    [[self appDelegate] setCurrentlySELEctedBlogItem:[[[self RSSParser]RSSItems]objectATindex:indexPath.row]];
    [self.appDelegate loadNewsDetails];
    [_tableView deSELEctRowATindexPath:indexPath animated: YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPorTraitUpsideDown);
}

- (void)dealloc {
    [_appDelegate release];
    [_toolbar release];
    [_tableView release];
    [_RSSParser release];
    [super dealloc];
}

@end

我找到了导致问题的代码行:

cell.textLabel.text = [[[[self RSSParser]RSSItems]objectATindex:indexPath.row]title];
cell.detailTextLabel.text = [[[[self RSSParser]RSSItems]objectATindex:indexPath.row]description];

如果我删除这些代码行,我无法重现错误.但是你可以想象它们对RSS @L_673_40@是必要的:).

解决方案吗

这是获取代码

#import "BlogRSSParser.h"
#import "BlogRSS.h"

@implementation BlogRSSParser

@synthesize currentItem = _currentItem;
@synthesize currentItemValue = _currentItemValue;
@synthesize RSSItems = _RSSItems;
@synthesize delegate = _delegate;
@synthesize retrieverQueue = _retrieverQueue;


- (id)init{
    self = [super init];
    if(self){
        _RSSItems = [[NSMutableArray alloc]init];
    }
    return self;
}

- (NSOperationQueue *)retrieverQueue {
    if(nil == _retrieverQueuE) {
        _retrieverQueue = [[NSOperationQueue alloc] init];
        _retrieverQueue.maxConcurrentOperationCount = 1;
    }
    return _retrieverQueue;
}

- (void)startProcess{
    SEL method = @SELEctor(fetchAndParseRSS);
    [[self RSSItems] removeAllObjects];
    NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self 
                                                                     SELEctor:method 
                                                                       object:nil];
    [self.retrieverQueue addoperation:op];
    [op release];
}

-(BOOL)fetchAndParseRSS{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [UIApplication sharedApplication].networkActivityInDicatorVisible = YES;

    //To suppress the leak in NSXMLParser
    [[NSURLCache sharedURLCache] setMemoryCapacity:0];
    [[NSURLCache sharedURLCache] setDiskCapacity:0];

    BOOL success = NO;
    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:YES];
    [parser setShouldReportNamespacePrefixes:YES];
    [parser setShouldResolveExternalEntities:NO];
    success = [parser parse];
    [parser release];
    [pool drain];
    return success;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
    if(nil != qualifiedName){
        elementName = qualifiedName;
    }
    if ([elementName isEqualToString:@"item"]) {
        self.currentItem = [[[BlogRSS alloc]init]autorelease];
    }else if ([elementName isEqualToString:@"media:thumbnail"]) {
        self.currentItem.mediaUrl = [attributeDict valueForKey:@"url"];
    } else if([elementName isEqualToString:@"title"] || 
              [elementName isEqualToString:@"description"] ||
              [elementName isEqualToString:@"link"] ||
              [elementName isEqualToString:@"guid"] ||
              [elementName isEqualToString:@"pubDate"]) {
        self.currentItemValue = [NSMutableString String];
    } else {
        self.currentItemValue = nil;
    }   
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if(nil != qName){
        elementName = qName;
    }
    if([elementName isEqualToString:@"title"]){
        self.currentItem.title = self.currentItemValue;
    }else if([elementName isEqualToString:@"description"]){
        self.currentItem.description = self.currentItemValue;
    }else if([elementName isEqualToString:@"link"]){
        self.currentItem.linkUrl = self.currentItemValue;
    }else if([elementName isEqualToString:@"guid"]){
        self.currentItem.guidUrl = self.currentItemValue;
    }else if([elementName isEqualToString:@"pubDate"]){
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
        self.currentItem.pubDate = [formatter dateFromString:self.currentItemValue];
        [formatter release];
    }else if([elementName isEqualToString:@"item"]){
        [[self RSSItems] addObject:self.currentItem];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)String {
    if(nil != self.currentItemvalue){
        [self.currentItemValue appendString:string];
    }
}

- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock{
    //Not needed for Now
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
    if(parseError.code != NSXMLParserDelegateAbortedParseError) {
        [UIApplication sharedApplication].networkActivityInDicatorVisible = NO;
        [(id)[self delegate] performSELEctorOnMainThread:@SELEctor(processHasErrors)
         withObject:nil
         waitUntilDone:NO];
    }
}



- (void)parserDidEndDocument:(NSXMLParser *)parser {
    [(id)[self delegate] performSELEctorOnMainThread:@SELEctor(processCompleted)
     withObject:nil
     waitUntilDone:NO];
    [UIApplication sharedApplication].networkActivityInDicatorVisible = NO;
}


-(void)dealloc{
    self.currentItem = nil;
    self.currentItemValue = nil;
    self.delegate = nil;

    [_RSSItems release];
    [super dealloc];
}

@end

解决方法

你应该做的是将获取的数据数组复制到ivar.然后从该ivar填充tableview,然后在processCompleted中将新数据复制到ivar并调用reloadData.这将使tableview不会处于您遇到的不一致状态.

@property (retain,nonatomiC) NSArray *sourceArray;

- (void)processCompleted{
    self.sourceArray = [[[[self RSSParser]RSSItems] copy] autorelease];
    [self toggleToolBarButtons:YES];
    [[self tableView]reloadData];
}

然后在填充tableview时参复制的数组.例如:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RSSItemCell"];
    if(nil == cell){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reusEIDentifier:@"RSSItemCell"]autorelease];
    }
    cell.textLabel.text = [[self.sourceArray objectATindex:indexPath.row]title];
    cell.detailTextLabel.text = [[self.sourceArray objectATindex:indexPath.row]description];
    cell.accessoryType = UITableViewCellAccessoryDisclosureInDicator;
    return cell;
}

并且在所有其他tableview委托方法中,您引用[[self RSSParser] RSSItems].

大佬总结

以上是大佬教程为你收集整理的iphone – 超出空阵列崩溃范围的索引’5′全部内容,希望文章能够帮你解决iphone – 超出空阵列崩溃范围的索引’5′所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。