Objective c 如何每20秒写入一次文件

Objective c 如何每20秒写入一次文件,objective-c,Objective C,我如何让这段代码每隔20秒将纬度/经度写入文件?多谢各位 // Save the longitude & latitude to file & check for errors NSString *newLocString = [NSString stringWithFormat:@"%s%f\n%s%f","Lat=",currentLocation.coordinate.latitude,"Long=",currentLocation.coordinate.longitude

我如何让这段代码每隔20秒将纬度/经度写入文件?多谢各位

// Save the longitude & latitude to file & check for errors
NSString *newLocString = [NSString stringWithFormat:@"%s%f\n%s%f","Lat=",currentLocation.coordinate.latitude,"Long=",currentLocation.coordinate.longitude];

NSString *path = @"var/Coordinates/myLocation.txt";
NSError *error = nil;

BOOL success = [newLocString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSString *status = success ? @"Success" : @"Failure";
if(success){

    UIAlertView *savedAlert = [[UIAlertView alloc] initWithTitle:@"Success!"message:@"The myLocation.txt file was updated." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [savedAlert show];

    NSLog(@"Done Writing: %@",status);
}
else{
    NSLog(@"Done Writing: %@",status);
    NSLog(@"Error: %@",[error localizedDescription]);
}

使用
n定时器
,每20秒触发一次:

NSTimer *timer = [NSTimer timerWithTimeInterval:20
                                         target:self
                                       selector:@selector( yourFunctionThatWritesToFile )
                                       userInfo:nil
                                        repeats:YES];
[timer fire];

使用
n定时器
,每20秒触发一次:

NSTimer *timer = [NSTimer timerWithTimeInterval:20
                                         target:self
                                       selector:@selector( yourFunctionThatWritesToFile )
                                       userInfo:nil
                                        repeats:YES];
[timer fire];

使用
n定时器
,每20秒触发一次:

NSTimer *timer = [NSTimer timerWithTimeInterval:20
                                         target:self
                                       selector:@selector( yourFunctionThatWritesToFile )
                                       userInfo:nil
                                        repeats:YES];
[timer fire];

使用
n定时器
,每20秒触发一次:

NSTimer *timer = [NSTimer timerWithTimeInterval:20
                                         target:self
                                       selector:@selector( yourFunctionThatWritesToFile )
                                       userInfo:nil
                                        repeats:YES];
[timer fire];


谢谢,您能告诉我如何将其添加到我的代码中吗?请将您在问题中编写的代码移动到函数中。然后将“yourFunctionThatWritesToFile”替换为函数名。您将拥有一个保存用户当前位置的属性,您可以在
CLLocationManager
委托中更新该属性,该属性将为您提供用户位置。我不会为您编写代码,如果您想学习,那是您的工作。我使函数void printFile(CLLocation*currentLocation),将函数所在的位置替换为“printFile(currentLocation);”&它可以工作。然后我在我的函数调用下添加以下内容:NSTimer*timer=[NSTimer timerWithTimeInterval:20 target:self selector:printFile(currentLocation)userInfo:nil repeats:YES];但获取错误“将void发送到不兼容类型SEL的参数”。现在我更改为:NSTimer*timer=[NSTimer timerWithTimeInterval:20 target:self selector:@selector(printFile)userInfo:nil repeats:YES];我现在得到警告“未声明的选择器‘printFile’”。请仔细阅读我的评论。“您将拥有一个保存用户当前位置的属性”。因此,您的函数应该是
-(void)printCurrentLocation
和选择器
@selector(printCurrentLocation)
。您不会将当前位置作为参数传递,它将是一个属性,因此您可以使用
self.currentLocation
访问它。谢谢,您能否向我演示如何将其添加到我的代码中?将您在问题中编写的代码移到函数中。然后将“yourFunctionThatWritesToFile”替换为函数名。您将拥有一个保存用户当前位置的属性,您可以在
CLLocationManager
委托中更新该属性,该属性将为您提供用户位置。我不会为您编写代码,如果您想学习,那是您的工作。我使函数void printFile(CLLocation*currentLocation),将函数所在的位置替换为“printFile(currentLocation);”&它可以工作。然后我在我的函数调用下添加以下内容:NSTimer*timer=[NSTimer timerWithTimeInterval:20 target:self selector:printFile(currentLocation)userInfo:nil repeats:YES];但获取错误“将void发送到不兼容类型SEL的参数”。现在我更改为:NSTimer*timer=[NSTimer timerWithTimeInterval:20 target:self selector:@selector(printFile)userInfo:nil repeats:YES];我现在得到警告“未声明的选择器‘printFile’”。请仔细阅读我的评论。“您将拥有一个保存用户当前位置的属性”。因此,您的函数应该是
-(void)printCurrentLocation
和选择器
@selector(printCurrentLocation)
。您不会将当前位置作为参数传递,它将是一个属性,因此您可以使用
self.currentLocation
访问它。谢谢,您能否向我演示如何将其添加到我的代码中?将您在问题中编写的代码移到函数中。然后将“yourFunctionThatWritesToFile”替换为函数名。您将拥有一个保存用户当前位置的属性,您可以在
CLLocationManager
委托中更新该属性,该属性将为您提供用户位置。我不会为您编写代码,如果您想学习,那是您的工作。我使函数void printFile(CLLocation*currentLocation),将函数所在的位置替换为“printFile(currentLocation);”&它可以工作。然后我在我的函数调用下添加以下内容:NSTimer*timer=[NSTimer timerWithTimeInterval:20 target:self selector:printFile(currentLocation)userInfo:nil repeats:YES];但获取错误“将void发送到不兼容类型SEL的参数”。现在我更改为:NSTimer*timer=[NSTimer timerWithTimeInterval:20 target:self selector:@selector(printFile)userInfo:nil repeats:YES];我现在得到警告“未声明的选择器‘printFile’”。请仔细阅读我的评论。“您将拥有一个保存用户当前位置的属性”。因此,您的函数应该是
-(void)printCurrentLocation
和选择器
@selector(printCurrentLocation)
。您不会将当前位置作为参数传递,它将是一个属性,因此您可以使用
self.currentLocation
访问它。谢谢,您能否向我演示如何将其添加到我的代码中?将您在问题中编写的代码移到函数中。然后将“yourFunctionThatWritesToFile”替换为函数名。您将拥有一个保存用户当前位置的属性,您可以在
CLLocationManager
委托中更新该属性,该属性将为您提供用户位置。我不会为您编写代码,如果您想学习,那是您的工作。我使函数void printFile(CLLocation*currentLocation),将函数所在的位置替换为“printFile(currentLocation);”&它可以工作。然后我在我的函数调用下添加以下内容:NSTimer*timer=[NSTimer timerWithTimeInterval:20 target:self selector:printFile(currentLocation)userInfo:nil repeats:YES];但获取错误“将void发送到不兼容类型SEL的参数”。现在我更改为:NSTimer*timer=[NSTimer timerWithTimeInterval:20 target:self selector:@selector(printFile)userInfo:nil repeats:YES];我现在得到警告“未声明的选择器‘printFile’”。请仔细阅读我的评论。“您将拥有一个保存用户当前位置的属性”。因此,您的函数应该是
-(void)printCurrentLocation
和选择器
@selector(printCurrentLocation)
。您不会将当前位置作为参数传递,它将是一个属性,因此您可以使用
self.currentLocation
访问它。这是用于NSA应用程序的吗?奇怪的问题!这是NSA的应用程序吗?奇怪的问题!这是什么