Creating a future date in swift with NSDate()

I’m getting an error: “Missing argument for parameter ‘coder’ in call” for the following code:

var components = NSDateComponents()     
components.setValue(1,forComponent: NSCalendarUnit.CalendarUnitMonth);
var expirationDate = NSCalendar.currentCalendar().dateByAddingComponents(components,       toDate: NSDate(),options:0)

The docs calls for:

  • Xcode Swift Go back to previous viewController (TableViewController)
  • Swift: why I can't call method from override init?
  • iOS 8 Swift Read Plist
  • Pass optional block or closure to a function in Swift
  • Getting back a date from a string
  • IOS - Running background task for update user location using swift
  • NSCalendar.currentCalendar().dateByAddingComponents(<#comps: NSDateComponents?#>, toDate: <#NSDate?#>, options: <#NSCalendarOptions#>)
    

    Can anyone see what I’m missing? I don’t see a parameter named ‘coder’ required.

    3 Solutions Collect From Internet About “Creating a future date in swift with NSDate()”

    that looks to be correct syntactically in Swift 1.2:

    var components = NSDateComponents()
    components.setValue(1, forComponent: NSCalendarUnit.CalendarUnitMonth);
    let date: NSDate = NSDate()
    var expirationDate = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: date, options: NSCalendarOptions(0))
    

    in Swift 2.0 you need to update this code a bit like this:

    let components: NSDateComponents = NSDateComponents()
    components.setValue(1, forComponent: NSCalendarUnit.Month);
    let date: NSDate = NSDate()
    let expirationDate = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: date, options: NSCalendarOptions(rawValue: 0))
    

    Syntax for Swift 3

    var components = DateComponents()
    components.setValue(1, forComponent: .month)
    let date: Date = Date()
    let expirationDate = Calendar.current.date(byAdding: components, to: date, options: [])
    

    Latest Swift 3 syntax:

    var components = DateComponents()
    components.setValue(1, for: .month)
    let date: Date = Date()
    let expirationDate = Calendar.current.date(byAdding: components, to: date)