2012-03-14 11 views
0

私は脱獄iPhoneのために開発しています。私は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(速度キロ/見出し)の列'。 "

関連する問題