xcode iPhone iOS 4 addTimeInterval 已弃用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3054914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:28:07  来源:igfitidea点击:

iPhone iOS 4 addTimeInterval deprecated

iphonexcodensdateios4

提问by I?igo Beitia

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

我正在addTimeInterval用于创建本地通知,但它现在似乎已被弃用(iOS 4)。

My code:

我的代码:

localNotif.fireDate = [now addTimeInterval:timeInterval];

Xcode's warning:

Xcode 的警告:

'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.

我应该用什么代替?谢谢。

回答by kennytm

The method has been renamed to -dateByAddingTimeInterval:.

该方法已重命名为-dateByAddingTimeInterval:.

localNotif.fireDate = [now dateByAddingTimeInterval:timeInterval];


In Swift 2.2:

在 Swift 2.2 中:

localNotif.fireDate = now.dateByAddingTimeInterval(timeInterval)


In Swift 3:

在 Swift 3 中:

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

回答by Reinhard

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