Shadow Shadow - 2 years ago 291
Swift Question

convert NSDateComponents to NSDate

How can I convert


let dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
dateFormatter.calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)

var dateii = NSData(toDate)// toDate from NSDateComponents error 1
let dateString = dateFormatter.stringFromDate(toDate) //error 2

Error 1:

Argument labels '(_:)' do not match any available overloads

Error 2:

Cannot convert value of type 'NSDateComponents' to expected argument type 'NSDate'

Answer Source

You are using NSData instead of NSDate. A typo.

As for your string, the date formatter transforms a date into a string, not date components. You have to first make a date out of the components.

let dateToDisplay = calendar.dateFromComponents(components)!

These errors would not have occurred if you had read the documentation or tried to understand relevant sample code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download