2017-12-02 24 views
-2

기본적으로 체중을 추적하는 앱을 만들고 있습니다. 스크린 샷의NSUserDefaults가있는 "bounds beyond bounds [0 .. 2]"문제가 있습니다.

enter image description here

짧은 설명 : 더 나은 시각적 접근을 위해, 나는 스크린 샷을 첨부 검은 색으로 표시 나는 인덱스 0, 1, 2, 3 (에 가중치를 저장하는있는 NSMutableArray를 선언 한 색깔). "초기"막대는이 MutableArray와는 아무런 관련이 없습니다. 가중치가 추가 될 때마다 나중에 사용하기 위해 NSUserDefaults에 저장됩니다.

달성하고자하는 것은 MutableArray의 모든 항목이 채워지면, 즉 모든 막대가 표시 될 때 인덱스 0이 제거되고 인덱스 1과 2가 "배치/1 개 위치로 왼쪽 '으로 이동 한 다음 인덱스의 새로운 무게를 추가 3.

내 문제는 :

[__NSArrayM objectAtIndexedSubscript:]: index 3 beyond bounds [0 .. 2]

: 모든 막대가 작성 후 새로운 무게를 추가하는 시간, 나는이 오류

이 오류는 배열이 비어 있음을 의미하는 것으로 알고 있는데 그 이유를 파악할 수 없습니다.

차트가로드되어있는 NSMutableArray를 초기화하는 코드입니다 :

if([weightUserDefaults objectForKey:@"weightMutableArray"] == nil){ // If no weight has ever been inserted then initialize array. 

    // We initialize the array, otherwise it crashes. 
    [weightMutableArray addObject:@"0"]; 
    [weightMutableArray addObject:@"0"]; 
    [weightMutableArray addObject:@"0"]; 
    [weightMutableArray addObject:@"0"]; 

    // We store this 1st time array initialization in the UserDefaults 
    [weightUserDefaults setValue:weightMutableArray forKey:@"weightMutableArray"]; 

    }else{ // If we have a weight from the Inaction, then show it in the graph 

    weightMutableArray = [[weightUserDefaults objectForKey:@"weightMutableArray"] mutableCopy];; 

    NSNumberFormatter *f = [[NSNumberFormatter alloc] init]; 

    PNBarChart * barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 0, optionsUIView.bounds.size.width, optionsUIView.bounds.size.height)]; 

     [barChart setYValues:@[ 
     [f numberFromString:[weightUserDefaults objectForKey:@"InitialWeight"]], 
     [f numberFromString:[weightMutableArray objectAtIndex:0]], 
     [f numberFromString:[weightMutableArray objectAtIndex:1]], 
     [f numberFromString:[weightMutableArray objectAtIndex:2]], 
     [f numberFromString:[weightMutableArray objectAtIndex:3]]]]; 

     [barChart strokeChart]; 
     [optionsUIView addSubview:barChart]; 
     [optionsUIView addSubview:addweightButton]; 

그리고 이것은 새로운 무게를 추가 할 IBAction를합니다. 나는 내가 조사한만큼이 문제가 여기에 있음을 확신한다.

for (int i=0;i < [weightMutableArray count]; i++){ // We start iterating in the array 
    if(![weightMutableArray[3] isEqualToString:@"0"]){ // If index 3 is not 0, means its been filled 
    [weightMutableArray removeObjectAtIndex:0]; // Then we remove the index 0 

    for (int j = 1;j<=3;j++)[weightMutableArray replaceObjectAtIndex:j-1 withObject:weightMutableArray[j]]; 
    [weightMutableArray insertObject:[[alertController textFields][0] text] atIndex:3]; // whatever its in index 1, put it in index 0. Then whatever its in index 2, put it in index 1, etc etc. 

      break; 
    }else if([weightMutableArray[i] isEqualToString:@"0"]){ //if any index is 0. 

    [weightMutableArray replaceObjectAtIndex:i withObject:[[alertController textFields][0] text]]; // then fill data from the UITextfield. 

      break; 
     } 
    } 

    // After everything is done, just store it back in the UserDefaults. 
    [weightUserDefaults setValue:weightMutableArray forKey:@"weightMutableArray"]; 
+0

루프에 '3'을 넣는 이유를 알 수 없습니다. '나'라는 뜻 이었습니까? – trungduc

+0

당신은'j'를 뜻합니까? 나는 weightMutableArray의 인덱스 1, 2, 3에서 iterare를 반복하고 싶다고 생각한다. 아이디어는 다음과 같습니다 : weightMutableArray [j-1] = weightMutableArray [j] for index 1,2,3. – pedrogr90

+0

아니,'if (! [weightMutableArray [3] isEqualToString : @ "0"])'. '3'은 'i'를 의미합니다. 권리? – trungduc

답변

0

먼저 첫 번째 항목을 제거하는 경우 :

[weightMutableArray removeObjectAtIndex:0]; // Then we remove the index 0 

배열 인덱스 0-2의 값 3 남아 있습니다.

는 제거하기 전에 배열은 다음과 같습니다 (코코아) 배열은 항상 첫 번째 항목은 다음과 같은 요소를 끌어 제거 (소형) 폐쇄

0: value1 
1: value2 
2: value3 
3: value4 

입니다. 이제 배열은 다음과 같습니다

0: value2 
1: value3 
2: value4 

따라서 인덱스 3 어떠한 항목이 없습니다 :

for (int j = 1;j<=3;j++) 

당신은 단순히 나머지 항목을 이동할 필요가 없습니다. 그것은 이미 완료되었습니다.

그러나 모든 점에서이 말을 해봅시다. 변경 가능한 모음 및 제로화의 전체 개념을 오해 한 것입니다. 0의 특별한 버전은 널 패턴입니다.

+0

그랬습니까. 이것은 내가 의미하는 바가 "개념을 오해 한 것"입니다. –

+0

예, 개념을 잘못 이해하고 있습니다. 나는 코딩 된 'for j'가 코코아가 나를 위해 그것을한다는 사실을 몰랐거나 고려하지 않았다. 그래서 나는 똑같은 것을 두 번하고 있었다. 고맙습니다 :) – pedrogr90

+0

코드의 문제는 처음부터 (배열 생성시), "비어있는"존재하지 않는 요소를 나타 내기 위해 0을 사용한다는 것입니다. –