iphone – 超出空阵列崩溃范围的索引’5′

iphone – 超出空阵列崩溃范围的索引’5′,第1张

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

所以这是我得到的错误:

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 Feed是必要的:).

有解决方案吗

这是获取代码:

#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′所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1014712.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存