运行时出现异常
self.year = value as! NSString
异常(exception):
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
关于ios - 无法在 Swift 中将 Anyobject 转换为 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30442987/