【问题标题】:iPhone iOS 4 addTimeInterval deprecatediPhone iOS 4 addTimeInterval 已弃用
【发布时间】:2011-03-04 13:14:29
【问题描述】:

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

我的代码:

localNotif.fireDate = [now addTimeInterval:timeInterval];

Xcode 的警告:

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

我应该改用什么? 谢谢。

【问题讨论】:

  • 始终阅读发行说明和 api diff 文档

标签: iphone xcode nsdate ios4


【解决方案1】:

该方法已重命名为-dateByAddingTimeInterval:

localNotif.fireDate = [now dateByAddingTimeInterval:timeInterval];

在 Swift 2.2 中:

localNotif.fireDate = now.dateByAddingTimeInterval(timeInterval)

在 Swift 3 中:

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

【讨论】:

    【解决方案2】:

    试试看:

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

    【讨论】:

      猜你喜欢
      • 2014-08-11
      • 2010-10-08
      • 2014-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多