기본적으로 체중을 추적하는 앱을 만들고 있습니다. 스크린 샷의NSUserDefaults가있는 "bounds beyond bounds [0 .. 2]"문제가 있습니다.
짧은 설명 : 더 나은 시각적 접근을 위해, 나는 스크린 샷을 첨부 검은 색으로 표시 나는 인덱스 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"];
루프에 '3'을 넣는 이유를 알 수 없습니다. '나'라는 뜻 이었습니까? – trungduc
당신은'j'를 뜻합니까? 나는 weightMutableArray의 인덱스 1, 2, 3에서 iterare를 반복하고 싶다고 생각한다. 아이디어는 다음과 같습니다 : weightMutableArray [j-1] = weightMutableArray [j] for index 1,2,3. – pedrogr90
아니,'if (! [weightMutableArray [3] isEqualToString : @ "0"])'. '3'은 'i'를 의미합니다. 권리? – trungduc