34

I'm using addTimeInterval for creating local notification but it seems that it is now deprecated (iOS 4).

My code:

localNotif.fireDate = [now addTimeInterval:timeInterval];

Xcode's warning:

'addTimeInterval:' is deprecated (declared at /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSDate.h:27)

What should I use instead? Thanks.

1
  • 2
    always read the release notes and api diff documentation Commented Jun 16, 2010 at 17:04

2 Answers 2

93

The method has been renamed to -dateByAddingTimeInterval:.

localNotif.fireDate = [now dateByAddingTimeInterval:timeInterval];

In Swift 2.2:

localNotif.fireDate = now.dateByAddingTimeInterval(timeInterval)

In Swift 3:

localNotif.fireDate = now.addingTimeInterval(timeInterval)
// or simply
localNotif.fireDate = now + timeInterval
0
11

try that:

NSDate *date = [NSDate date];     // current date
NSTimeInterval delta = 60 * 1;    // one minute later
if ([date respondsToSelector:@selector(dateByAddingTimeInterval:)]) {
date = [date dateByAddingTimeInterval:delta];
}
else {
   date = [date addTimeInterval:delta];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.