2012-03-14 3 views
0

탈옥 아이폰 용으로 개발 중입니다. 나는 iOS 5 권의 책을 읽고있다. 현재 책의 위치 코드를 사용하고 약간 변경했습니다. 코드에서 변경 한 점은 텍스트 파일에 위치를 쓰고 있다는 것입니다. 그러나 프로그램이 충돌합니다. 수정하지 않은 동일한 프로그램이 완벽하게 작동하지만. 질문에텍스트 파일에 위치 쓰기 시도

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = 2.0f; 

    [locationManager startUpdatingLocation]; 
} 






- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
    FILE *file = fopen("/usr/locations.txt", "a"); 

    if (!file) 
    { 
     fprintf(file, "Error opening file"); 
    } 

    if (startingPoint == nil) 
     self.startingPoint = newLocation; 

    NSString *latitudeString = [NSString stringWithFormat:@"%g\u00B0", 
           newLocation.coordinate.latitude]; 
    latitudeLabel.text = latitudeString; 


    NSNumber *latitude = [[NSNumber alloc] initWithDouble:newLocation.coordinate.latitude]; 
    NSNumber *longitude = [[NSNumber alloc] initWithDouble:newLocation.coordinate.longitude]; 


    NSString *longitudeString = [NSString stringWithFormat:@"%g\u00B0", 
           newLocation.coordinate.longitude]; 
    longitudeLabel.text = longitudeString; 


    NSString *horizontalAccuracyString = [NSString stringWithFormat:@"%gm", 
              newLocation.horizontalAccuracy]; 
    horizontalAccuracyLabel.text = horizontalAccuracyString; 


    NSString *altitudeString = [NSString stringWithFormat:@"%gm", 
           newLocation.altitude]; 
    altitudeLabel.text = altitudeString; 

    NSString *verticalAccuracyString = [NSString stringWithFormat:@"%gm", 
             newLocation.verticalAccuracy]; 
    verticalAccuracyLabel.text = verticalAccuracyString; 


    CLLocationDistance distance = [newLocation 
            distanceFromLocation:startingPoint]; 
    NSString *distanceString = [NSString stringWithFormat:@"%gm", distance]; 
    distanceTraveledLabel.text = distanceString; 


    struct tm *local; 
    time_t t; 

    t = time(NULL); 
    local = localtime(&t); 

    fprintf(file, "Local time and Date: %s ", asctime(local)); 

    //----------------- ------------------------------------------------------------ 

    fprintf(file, "Latitude = %lf, Longitude = %lf \n", 
      newLocation.coordinate.latitude, newLocation.coordinate.longitude); 


    fclose(file); 

} 
+0

파일 시스템 액세스에 관한 [Apple 설명서] (https://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/Introduction/Introduction.html)를 읽으셨습니까? 읽어봐. – JoePasq

+0

이제 콘솔에서만 위치를 인쇄하고 싶습니다. 그러나 나는 그렇게 할 수 없었다. 내 개발 IDE는 Xcode 4.2이고 iOS 버전은 5.0.1입니다. –

답변

0

가장 최근 댓글이 대신 콘솔에 인쇄 할 말합니다 : 여기

는 코드입니다. 콘솔에 인쇄하려면 NSLog()을 사용하십시오. 객체의 형식 지정자 %@을 사용하는 경우 객체를 대체하는 문자열은 객체의 description 메소드입니다.

콘솔을 사용

NSLog(@"New location: %@", newLocation); 
NSLog(@"Old location: %@", oldLocation); 

형태 '위도의 문자열 "CLLocation 반환에 대한 설명 방법, 경도 +/- 정확도 m (속도 시속/호) @ 날짜 - 시간에 위치를 인쇄하려면 '. "