Iphone 尝试在文本文件中写入位置

Iphone 尝试在文本文件中写入位置,iphone,cllocationmanager,Iphone,Cllocationmanager,我正在为越狱iPhone开发。我正在读这本书。目前,我使用了书中的位置代码,并对其进行了轻微更改。我在代码中所做的更改是,我正在文本文件中写入位置。但是程序崩溃了。虽然没有任何修改的同一个程序工作得很好 代码如下: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.locationM

我正在为越狱iPhone开发。我正在读这本书。目前,我使用了书中的位置代码,并对其进行了轻微更改。我在代码中所做的更改是,我正在文本文件中写入位置。但是程序崩溃了。虽然没有任何修改的同一个程序工作得很好

代码如下:

- (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);

}

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

要在控制台上打印位置,请使用

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

上的描述方法返回“一个字符串,格式为‘纬度、经度+/-精度m(速度kph/航向)@date-time’”

您读过文件系统访问吗?读一下。现在,我只想在控制台上打印位置。但我一直未能做到这一点。我的开发IDE是Xcode 4.2,iOS版本是5.0.1