시간이 double이라고합니다. 내가 다른 클래스의 메소드에 두 배를 전달할 수 있습니다 이유를 확인할 수 있지만, 요구하지 수 같은 방법은 존재 클래스에서 더블 말했다내 메소드가 동일한 변수에 대해 다른 값을 반환하는 이유
내 헤더 파일은 다음과 같이 같습니다.
@property (nonatomic) double time;
을
내 구현은 이런 식입니다 :
@implementation MainVewController
@synthesize time;
- (void) viewDidLoad
{
startTime = NSDate.date;
}
- (double) returnTime {
NSLog (@"time is disappearing?? %f", time);
return time;
}
- (double) logTime {
NSLog (@"for some reason, this one is working and returns a value %f", time);
return time;
}
내 다른 클래스 내 배를 요구한다 :
@synthesize mainViewController = _mainViewController ;
- (MainViewController *)mainViewController {
if (!_mainViewController) _mainViewController = [[MainViewController alloc] init];
return _mainViewController;
}
- (BOOL)getTime {
double timeGotten = [self.mainViewController returnTime];
// why does this return 0?
return TRUE;
}
시간 변수는 지속적으로 MainVewController 내에서 업데이트되는 :
time = [[NSDate date] timeIntervalSinceDate: startTime];
두 방법과 관련하여 time 변수가 선언 된 위치는 어디입니까? –
다른 곳에서 어떻게 묻지 않았습니까? 참고로, 너는 두 배로 돌아 오지 않는다 .... 너의 두 배'time '은 존재하지 않을지도 모른다. 중단 점을 설정하십시오. 먼저 self.mainViewController가 있는지 확인한 다음 시간 값을 확인하고'timeGotten'이 필요로 할 경우를 대비하여 반환하십시오. – Legolas
returnTime과 logTime은 표시된 코드에 대해 동일한 값을 반환합니다. 우리가 볼 수있는 한, 당신은 결코 시간의 가치를 설정하지 않습니다. –