2013-08-01 2 views
0

그래서 작업 엔티티 클래스 (핵심 데이터)가 있고 해당 문자열 (timeIntervalString) 중 하나에 대한 설정자를 덮어 쓰려고하므로 세부 텍스트 레이블에 표시 될 수 있습니다 표보기 셀의.잘 모르겠 음이 EXC_BAD_ACCESS 오류

-(NSString *)timeIntervalString{ 

    NSUInteger seconds = (NSUInteger)round(self.timeInterval); 
if ((seconds/3600) == 0){ 
    if (((seconds/60) % 60) == 1) { 
     self.timeIntervalString = [NSString stringWithFormat:@"%u MIN", ((seconds/60) % 60)]; 
    } else { 
     self.timeIntervalString = [NSString stringWithFormat:@"%u MINS", ((seconds/60) % 60)]; 
    } 
} else if ([self.conversionInfo hour] == 1) { 
    if (((seconds/60) % 60) == 0){ 
     self.timeIntervalString = [NSString stringWithFormat:@"%u HR", (seconds/3600)]; 
    } else if (((seconds/60) % 60) == 1) { 
     self.timeIntervalString = [NSString stringWithFormat:@"%u HR %u MIN", (seconds/3600), ((seconds/60) % 60)]; 
    } else { 
     self.timeIntervalString = [NSString stringWithFormat:@"%u HR %u MINS", (seconds/3600), ((seconds/60) % 60)]; 
    } 
} else { 
    if (((seconds/60) % 60) == 0) { 
     self.timeIntervalString = [NSString stringWithFormat:@"%u HRS ", (seconds/3600)]; 
    } else if (((seconds/60) % 60) == 1){ 
     self.timeIntervalString = [NSString stringWithFormat:@"%u HRS %u MIN", (seconds/3600), ((seconds/60) % 60)]; 
    } else { 
     self.timeIntervalString = [NSString stringWithFormat:@"%u HRS %u MINS", (seconds/3600), ((seconds/60) % 60)]; 
    } 
} 
return self.timeIntervalString; 
: 여기

내 코드는 ...

enter image description here

에서 [작업은 timeIntervalString] 37355 같은 때까지 계속된다 : 어떤 이유로 나는이 같은 EXC_BAD_ACCESS 오류를 받고 있어요

}

아이디어가 있으십니까?

+0

반 관련 디자인 질문 :이 메서드 정의 외부에서 timeIntervalString 값을 설정 했습니까? timeIntervalString 값을 반환하는 것보다 읽기 전용 속성으로 표시해야하는지 여부에 대해서도 생각해보십시오. –

+0

아니오 실제로 설정하지 마십시오. 표시 만합니다. 하지만 시간 간격을 설정하지 마십시오 – EvilAegis

+0

Esker가 말한 줄에 따라이 값을'_timeIntervalString'에 저장할 필요는 없을 것입니다. 귀하의 속성을 읽기 전용 및 동적으로 선언하고 지정을 건너 뛰고 서식이 지정된 값을 반환합니다. 이렇게하면 불필요한 인스턴스 변수에서 일부 메모리를 절약 할 수 있습니다. –

답변

5

return self.timeIntervalString은 동일한 timeIntervalString 메서드를 재귀 적으로 호출합니다.

아마도 원하는 것은 return _timeIntervalString입니다.

설명 : self.timeIntervalString은 과 같은 의미이며 여기에서 정의한 -(NSString *)timeIntervalString 메서드를 호출합니다. 을 변경하면 속성 접근자를 재귀 적으로 호출하는 대신 인스턴스 변수에 직접 액세스 할 수 있습니다. 이것은 사용자가 작성한 사용자 정의 특성 접근 자 메소드에서 따라야하는 일반적인 패턴입니다.

편집 : 당신의 .H 파일에서

:

@property (readonly) NSString *timeIntervalString; 

의견 토론을 기반으로, 읽기 전용 속성으로이를 표시하고 실제로 값을 설정 결코 더 나은 것 .m 파일에서 :

-(NSString *)timeIntervalString { 
    NSString *value; 
    // insert here the body of your timeIntervalString method, as you 
    // originally wrote it, but replace all occurences of: 
    // self.timeIntervalString = ... 
    // with this: value = ... 
    return value; 
} 
+0

그 말이 맞습니다! 그러나 xcode는 "선언되지 않은 식별자 _timeIntervalString 사용"을 제공합니다. 핵심 데이터와 @dynamic timeIntervalString으로 설정 되었기 때문에 신고되지 않았습니다. 생각? – EvilAegis

+0

.m 파일의 @implementation 줄 바로 뒤에'@synthesize timeIntervalString = _timeIntervalString'이 필요할 수도 있습니다. 그러나 개선 된 솔루션에 대한 나의 편집 된 답변을 참조하십시오. –

+0

실제로 변수는 자동으로 밑줄 표기법으로 합성되므로, 강력한 속성으로 선언 된 경우'@synthesize timeIntervalString'이 작동합니다. – mrosales

관련 문제