나는이 응용 프로그램에 MVC를 구현하기 위해 작업 해 왔으며 프로그래밍을 가르쳐 왔으며 지난 몇 일간 EXC_BAD_ACCESS 오류가 발생했습니다. 나는 꽤 많이 읽었으며 디버거 설정으로 인해 내 모델 객체의 속성에 내 컨트롤러 객체에서 호출 할 때 보유 메시지가 이미 할당 취소 된 인스턴스입니다. 나쁜 액세스 오류가 표시된 줄은 항상 오류가있는 곳이 아니지만 행을 주석으로 처리하여 내가 그 자리를 찾은 것 같아요.목표 C 속성 할당하기
내 컨트롤러로 내 AppDelegate에 클래스를 사용하고, 그리고 버튼을 누를 때 다음 함수가 호출됩니다 :
-(void)makeCodesandNamesArray:(long)popvalue{
NSString *codesNames = [[NSString alloc] init];
switch ((int)popvalue) {
case 0:
codesNames = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource: @"NEWD65Lab" ofType: @"txt"] encoding:NSUTF8StringEncoding error:NULL];
break;
case 1:
codesNames = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource: @"NEWD50Lab" ofType: @"txt"] encoding:NSUTF8StringEncoding error:NULL];
break;
case 2:
codesNames = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource: @"NEWALab" ofType: @"txt"] encoding:NSUTF8StringEncoding error:NULL];
break;
case 3:
codesNames = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource: @"NewCodesandNames" ofType: @"txt"] encoding:NSUTF8StringEncoding error:NULL];
break;
default:
NSLog(@"Selection Index outside bounds of popup somehow");
break;
}
NSArray *colorArray = [[NSArray alloc]initWithArray:[codesNames componentsSeparatedByString:@"\n"]];
self.CodesandNames = colorArray;}
:
내 모델 클래스에서
- (IBAction)makeChart:(id)sender {
[self.myModel makeCodesandNamesArray:self.popupButton.indexOfSelectedItem];
NSLog(@"%@",[self.myModel.CodesandNames objectAtIndex:3]);
[sender setEnabled:NO];
NSLog(@"Done!!");}
, 나는 makeCodesandNamesArray 방법을
모델의 헤더 파일에 CodesandNames에 대한 @property가 있고 모델에서 컨트롤러에 올바르게 연결되어 있습니다.
컨트롤러의 NSLog에 내 오류가 표시되고 디버거에서 "[__NSArrayI retain] : 해제 된 인스턴스 0x1e0112200으로 보낸 메시지"라는 메시지를 표시합니다. 특정 인스턴스 0x1e0112200을 찾을 수 없었지만 NSLog를 주석 처리 할 때이 오류가 발생하지 않습니다. 나는 또한 CodesandNamesproperty를 매개 변수로 사용하고 동일한 오류를받는 컨트롤러 동작 내에서 다른 메서드 호출을 사용했습니다.
속성을 정의하고 NSLog에서 호출하려고 시도하는 사이에 할당이 해제 된 것처럼 보이지만 수행 할 수있는 내용이 표시되지 않습니다.
또한 모델에서 makeCodesandNamesArray를 사용하여 colorArray를 반환하고 컨트롤러의 self.myModel로 설정했지만 동일한 오류가 발생했습니다. 마지막으로, makeCodesandNamesArray가 colorArray를 리턴하고 NSLog에서 호출했을 때 오류가 없었고 어레이가 정상적으로 표시되었습니다.
이 문제에 대한 도움을 주시면 감사하겠습니다.
당신은 변수 선언을 보여줄 수 있을까? 그것은 strong/retain으로 참조되어야합니다. 약한 경우 배열이 유지되지 않습니다. – Lepidopteron
그래, 그것은 @property (assign) NSArray * CodesandNames; 나는 아래의 Tiago의 충고에 따라 그것을 변경했다. 내가 실수로하지 않은 소문자로 시작하는 표준으로 변경하는 것을 포함한다. 이 문제를 볼 시간을내어 주셔서 감사합니다 – nintandrew