开发者

iPhone iOS 4 addTimeInterval deprecated

开发者 https://www.devze.com 2023-01-03 21:39 出处:网络
I\'m using addTimeInterval for creating local notification but it seems that it is now deprecated (iOS 4).

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

My code:

localNotif.fireDate = [now addTimeInterval:time开发者_如何学CInterval];

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.


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


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];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消