I've written such extension for String :
extension String {
var asDate : NSDate! {
print(self)
let styler = NSDateFormatter()
styler.dateFormat = "dd MMM yyyy"
print(styler.dateFromString(self))
if let aStyler = styler.dateFromString(self) {
return aStyler
} else {
return NSDate()
}
}
}
I've noticed that it only works when English language is set on the device. However, it doesn't work with Russian, for example. Why?
It takes a string, e.g. "10 Apr 2016", and it should convert this string to date. However, styler.dateFromString(self)
is always nil
when the language is not English. I've checked that self (the string that I want to convert) matches the unicode format. However, I don't understand why styler.dateFromString(self)
always returns nil.