2013-04-06 4 views
0
  1. 내 응용 프로그램 충돌 ramdomly _storeArray.
  2. viewDidLoad에서 xml 파일을 구문 분석 한 다음 사전을 추가하는 [self loadUrl] 메서드를 사용합니다. [_storeArray addObject:dictionary];
  3. 어떻게 든 회전식 컨베이어 reusingView 처음 호출 후 [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

+0

감사 @Kerni, 느릅 나무 버튼이 명확하게 코드를 보여줍니다 눌러합니까? – Marckaraujo

+0

_storeArray가 nil이 아니기 때문에 객체가 포함 된 것은 아닙니다. _storeArray에 객체를 어디에 추가합니까? 빈 배열이므로 objectAtIndex : 0이 존재하지 않습니다. –

답변

1
  1. _storeArray 만 검사 변수가 존재하는 경우 만, 존재하지 않는 경우에 대한 현재 체크 실제로 거기에있는 모든 요소. 그러나 검사가 끝난 후에는 배열에 요소가 하나 있다고 가정합니다.

    그래서이

    if (_storeArray) { 
    

    을 변경 또한

    if (_storeArray && [_storeArray count] > 0) { 
    
  2. 당신이 사전이 photo1 등의 키를해야합니다하지만하지 않으면 것은, 생성 된 URL이 될 것으로 가정합니다 비어 있고 결과적으로보기에는 URL이 없을 수도 있습니다.

    다음과 같이 나는 그것을 바꿀 것 (NSLog IT)

    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

0

내가, 당신의 구문 분석이 모든 전에 일어날 않는 한 가지를 알아야하고 성공적으로 배열을 반환 배열이 될 것으로 보인다 때문에 여기 문제. 첫 번째 물체를 직접 꺼낼 수는 없으므로 수비를 계속해야합니다. 이렇게하면됩니다.

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