سؤال

I'm not using ARC so I can't use weak. Please let me know, that what can I do to not allow the NSTimer to retain the target which in my case is self.

هل كانت مفيدة؟

المحلول

You might just go ahead and create a version of NSTimer that does not retain its target

+ (NSTimer *) scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo
{
    NSWeakTimerTarget* timerTarget = [[NSWeakTimerTarget alloc] init];
    timerTarget.target = aTarget;
    timerTarget.selector = aSelector;
    timerTarget.timer = [NSTimer scheduledTimerWithTimeInterval:ti target:timerTarget selector:@selector(fire) userInfo:userInfo repeats:yesOrNo];
    return timerTarget.timer;
}

from https://gist.github.com/bendytree/5674709

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top