在 Swift 中无法将 Anyobject 转换为 NSString

Fail to make Anyobject to NSString in Swift

我运行的时候出现异常

self.year = value as! NSString 

异常:

 Could not cast value of type '__NSDate' (0x1051bd6a0) to 'NSString' (0x10564d8e0).

我错过了什么?

var  datePicker = ActionSheetDatePicker(title: "Date:",datePickerMode: UIDatePickerMode.Date, selectedDate: NSDate(),doneBlock: { 

   picker, value, index in

   self.year = value as! NSString 

   println("year = \(year)")
   println("value = \(value)")

   return
   }, cancelBlock: { ActionStringCancelBlock in return }, origin: sender.superview!.superview)

我认为您必须通过您选择的值将日期转换为字符串

let formatter: DateFormatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd-HH-mm-ss"
let dateStr: String = formatter.string(from: value as! Date)

您可以使用任何其他格式,例如:"yyyy" 或 "HH:mm:ss" 等。

并将其设置为您的年份变量

self.year = dateStr