代码之家  ›  专栏  ›  技术社区  ›  Iñigo Beitia

iPhone iOS 4 AddTimeInterval已弃用

  •  34
  • Iñigo Beitia  · 技术社区  · 16 年前

    我在用 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)
    

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

    2 回复  |  直到 10 年前
        1
  •  93
  •   kennytm    10 年前

    方法已重命名为 -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
  •  11
  •   Reinhard    15 年前

    试试看:

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