2012-03-14 76 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 2012-03-14 18:30:58

+0

现在,我只想在控制台上打印位置。但是我还没有做到这一点。我的开发IDE是Xcode 4.2,iOS版本是5.0.1 – 2012-03-24 04:18:39

回答

0

你对这个问题最近评论说,你要打印到控制台。要在控制台上打印,请使用NSLog()。当您使用对象%@的格式说明符时,替换对象的字符串是对象的description方法。

要在控制台使用

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

CLLocation返回““的形式纬度的字符串的描述方法,经度+/-精度米(速度KPH /航向)@日期时间打印位置'。“