2013-04-06 100 views
0
  1. 我的应用程序在_storeArray中崩溃。
  2. 在viewDidLoad中,有一个方法[self loadUrl]解析xml文件,然后添加一个字典[_storeArray addObject:dictionary];
  3. 当旋转木马重新使用查看呼叫,然后[self loadUrl]时发生崩溃。
  4. 我已添加if(_storeArray)但它仍然崩溃。

这是例外:NSRangeException随机崩溃应用程序

2013-04-06 09:33:35.254 Semhora[7347:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' 

这是代码:

- (void)loadURL:(NSString *)newURL{ 

    // Create a success block to be called when the asyn request completes 
    TBXMLSuccessBlock successBlock = ^(TBXML *tbxmlDocument) { 


     [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; 
     // If TBXML found a root node, process element and iterate all children 
     if (tbxmlDocument.rootXMLElement) 
     { 
      // Obtain root element 
      TBXMLElement * root = tbxml.rootXMLElement; 
      if (root) 
      { 
       [_storeArray removeAllObjects]; 
       TBXMLElement * elem_PLANT = [TBXML childElementNamed:@"principal" parentElement:root]; 
       while (elem_PLANT !=nil) 
       { 
        TBXMLElement * elem_title = [TBXML childElementNamed:@"title" parentElement:elem_PLANT]; 
        NSString *titleName = [TBXML textForElement:elem_title]; 

//     TBXMLElement * elem_artist = [TBXML childElementNamed:@"text" parentElement:elem_PLANT]; 
//     NSString *artistName = [TBXML textForElement:elem_artist]; 

        TBXMLElement * elem_thumb = [TBXML childElementNamed:@"thumb_url" parentElement:elem_PLANT]; 
        NSString *thumbName = [TBXML textForElement:elem_thumb]; 

        TBXMLElement * elem_photo1 = [TBXML childElementNamed:@"photo1" parentElement:elem_PLANT]; 
        NSString *photo1Name = [TBXML textForElement:elem_photo1]; 

        TBXMLElement * elem_photo2 = [TBXML childElementNamed:@"photo2" parentElement:elem_PLANT]; 
        NSString *photo2Name = [TBXML textForElement:elem_photo2]; 

        TBXMLElement * elem_photo3 = [TBXML childElementNamed:@"photo3" parentElement:elem_PLANT]; 
        NSString *photo3Name = [TBXML textForElement:elem_photo3]; 

        TBXMLElement * elem_photo4 = [TBXML childElementNamed:@"photo4" parentElement:elem_PLANT]; 
        NSString *photo4Name = [TBXML textForElement:elem_photo4]; 

        TBXMLElement * elem_photo5 = [TBXML childElementNamed:@"photo5" parentElement:elem_PLANT]; 
        NSString *photo5Name = [TBXML textForElement:elem_photo5]; 

        NSDictionary *dictionary = [[NSDictionary alloc]initWithObjects:@[titleName, thumbName, photo1Name, photo2Name, photo3Name, photo4Name, photo5Name] forKeys:@[@"title", @"thumb_url", @"photo1", @"photo2", @"photo3", @"photo4", @"photo5",]]; 
        elem_PLANT = [TBXML nextSiblingNamed:@"principal" searchFromElement:elem_PLANT]; 
        [_storeArray addObject:dictionary]; 
        [self startLoading:dictionary]; 
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [_carousel reloadData]; }]; 
       } 


      } 

     } 

    }; 

    // Create a failure block that gets called if something goes wrong 
    TBXMLFailureBlock failureBlock = ^(TBXML *tbxmlDocument, NSError * error) { 
     NSLog(@"Error! %@ %@", [error localizedDescription], [error userInfo]); 
    }; 

    // Initialize TBXML with the URL of an XML doc. TBXML asynchronously loads and parses the file. 
    tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:newURL] 
           success:successBlock 
           failure:failureBlock]; 

} 

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view 
{ 
    //create new view if no view is available for recycling 
    if (view == nil) 
    { 
     FXImageView *imageView = [[FXImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)]; 
     imageView.contentMode = UIViewContentModeScaleAspectFit; 
     imageView.asynchronous = YES; 
     imageView.reflectionScale = 0.5f; 
     imageView.reflectionAlpha = 0.25f; 
     imageView.reflectionGap = 10.0f; 
     imageView.shadowOffset = CGSizeMake(0.0f, 2.0f); 
     imageView.shadowBlur = 5.0f; 
     imageView.cornerRadius = 10.0f; 
     view = imageView; 
    } 

    //show placeholder 
    ((FXImageView *)view).processedImage = [UIImage imageNamed:@"placeholder.png"]; 

    //set image 

    NSLog(@"%@", _storeArray); 
    if (_storeArray) { 

     NSDictionary *tempdic = [_storeArray objectAtIndex:0]; 

     switch (index) { 
      case 0: 
       [(FXImageView *)view setImageWithContentsOfURL:[NSURL URLWithString:[tempdic objectForKey:@"photo1"]]]; 
       break; 
      case 1: 
       [(FXImageView *)view setImageWithContentsOfURL:[NSURL URLWithString:[tempdic objectForKey:@"photo2"]]]; 
       break; 
      case 2: 
       [(FXImageView *)view setImageWithContentsOfURL:[NSURL URLWithString:[tempdic objectForKey:@"photo3"]]]; 
       break; 
      case 3: 
       [(FXImageView *)view setImageWithContentsOfURL:[NSURL URLWithString:[tempdic objectForKey:@"photo4"]]]; 
       break; 
      case 4: 
       [(FXImageView *)view setImageWithContentsOfURL:[NSURL URLWithString:[tempdic objectForKey:@"photo5"]]]; 
       break; 
      default: 
       break; 
     } 
    } 

    return view; 
} 
+0

我只是不能编辑此代码以清楚地显示... – Marckaraujo 2013-04-06 12:48:49

+0

谢谢@Kerni,你是否按下清楚地显示代码? – Marckaraujo 2013-04-06 12:58:12

+0

仅仅因为_storeArray不是零并不意味着它包含对象。你在哪里添加对象到_storeArray中?它是一个空数组,因此objectAtIndex:0不存在。 – 2013-04-06 12:59:37

回答

1
  1. 目前的检查_storeArray只检查,如果变量存在,但如果有实际上有任何元素在那里。但是在检查之后,你只要假设数组中有1个元素没有检查过,如果这是真的。

    所以改变这种

    if (_storeArray) { 
    

    这样:

    if (_storeArray && [_storeArray count] > 0) { 
    
  2. 另外你假设字典将有一个键photo1等,但如果不是,则生成的URL会为空,因此该视图也可能会得到一个零URL。

    我会如下改变:

    NSDictionary *tempdic = [_storeArray objectAtIndex:0]; 
    NSString *tempObjKey = nil; 
    
    switch (index) { 
        case 0: 
         tempObjKey = @"photo1"; 
         break; 
        case 1: 
         tempObjKey = @"photo2"; 
         break; 
        case 2: 
         tempObjKey = @"photo3"; 
         break; 
        case 3: 
         tempObjKey = @"photo4"; 
         break; 
        case 4: 
         tempObjKey = @"photo5"; 
         break; 
        default: 
         break; 
    } 
    
    if (tempObjKey && [tempdic objectForKey:tempObjKey]) 
        [(FXImageView *)view setImageWithContentsOfURL:[NSURL URLWithString:[tempdic objectForKey:tempObjKey]]]; 
    } 
    
+0

感谢Kerni,它确实有效。 – Marckaraujo 2013-04-06 13:18:06

0

我需要知道一两件事,那您的分析之前这一切发生,并成功返回一个数组(NSLog的话),因为数组似乎是这里的问题。你不能直接拿出第一个对象,你必须保持防守,所以做这样的事情。

if (_storeArray) { 

    if(_storeArray.count >0) { 

    NSDictionary *tempdic = [_storeArray objectAtIndex:0]; 

    switch (index) { // all ur stmts } 
    }} 
+0

我的XML在viewDidLoad [self loadUrl]中解析调用,当它完成时添加[_storeArray addObject:dictionary],但有时轮播在XML完成操作之前调用该方法。 – Marckaraujo 2013-04-06 13:14:09