Objective c NSTimer崩溃

Objective c NSTimer崩溃,objective-c,Objective C,我一直在寻找答案,但我觉得什么都不管用 我有一个NSTimer作为Appdelegate的属性,这个NSTimer应该在任何时候启动他的操作,即使应用程序在后台(它是一个本地化应用程序-因此可以永远运行) 代码如下: if(conectar){ self.myTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0

我一直在寻找答案,但我觉得什么都不管用

我有一个NSTimer作为Appdelegate的属性,这个NSTimer应该在任何时候启动他的操作,即使应用程序在后台(它是一个本地化应用程序-因此可以永远运行)

代码如下:

if(conectar){
            self.myTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
                                                            target:self
                                                          selector:@selector(abrirPresencia)
                                                          userInfo:nil
                                                           repeats:NO]retain];
        }
        else{
            self.myTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
                                                    target:self
                                                  selector:@selector(cerrarPresencia)
                                                  userInfo:nil
                                                           repeats:NO]retain];
        }
10秒后,应用程序崩溃了,我在两个方法中都有断点,它没有停止,好像这个方法甚至没有被调用,而不是触发它崩溃的方法

方法如下:

-(void)cerrarPresencia{
NSLog(@"SOY EL TIMERRRRR!!!! %@");
//[[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground
if (YES){
    [self.location stopUpdatingLocation];
    [self disconnect];

}
else{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Ups!"
                                                        message:@"Según tu perfil querrías estar desconectado pero como lo estás usando hemos incluido este horario. Para cambiarlo sólo tienes que ir a editar perfil."
                                                       delegate:self 
                                              cancelButtonTitle:@"Ok" 
                                              otherButtonTitles:nil];
    [alertView show];

    [alertView release];
}
}

}

该物业为:

@property (nonatomic, retain)NSTimer *myTimer;

有什么想法吗?

选择器错误,必须采用以下形式:

-(无效)声明:(n计时器*)计时器

选择器还需要指定带有尾随冒号的参数:
selector:@selector(abrirpresenscia)

从Apple API文档:

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
计时器启动时要发送到目标的消息。选择器必须具有以下签名:

- (void)timerFireMethod:(NSTimer*)theTimer
还记得你在这条线上得到的编译器警告吗

警告:转换“%”多于数据参数[-Wformat,7]

?


注意那些警告。您正在使用包含格式说明符
%@
的参数调用
NSLog
,这使它认为还会有更多的参数。如果不传递这些进一步的参数,该函数仍会在预期参数所在的位置查找,发现垃圾,并导致崩溃。

您能发布这些方法吗?崩溃日志中有什么错误?如果正在调用的方法有一个(或多个)参数,则应在选择器的末尾添加冒号:。你能按@Vladimir的要求做并发布方法吗?很可能这是一个SIGABRT。尝试在选择器名称中添加“:”。例如:(Cerrapresentia:)你也可以粘贴myTimer的属性吗?我添加了你建议的更改-(void)Cerrapresentia:(NSTimer*)timer{selector:@selector(Cerrapresentia:)我也得到了同样的错误…先生,这是很好的发现!@David Shaikh,尝试从两种方法中删除%@行,看看会发生什么。如果您仍然发生崩溃,请包括崩溃日志。天哪!就是这样!感谢所有人,特别是@Josh Caswell
- (void)timerFireMethod:(NSTimer*)theTimer
NSLog(@"SOY EL TIMERRRRR!!!! %@");