我想要实现的是将生日字符串转换为以下格式:08/15/2014
到NSDate
实例。然后将NSDate
转换回String
,但转换为ISO format
。
为什么我收到此错误消息? 无法将表达式的类型'$ T3'转换为'$ T4'
// birthdateString = 08/15/2014
let birthdateString = KeychainManager.getString(KeychainManager.StaticVars.kFbBirthdate)
let birthdateAsNSDate = Date.parseFacebookBirthdate(birthdateString)
let birthdateAsISOString = Date.getStringFromDate(birthdateAsNSDate)
日期转换类
class Date {
class func parseFacebookBirthdate(birthdateString: String) -> NSDate {
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
dateFormatter.timeZone = NSTimeZone(name: "UTC")
return dateFormatter.dateFromString(birthdateString)
}
class func getStringFromDate(date: NSDate) -> String {
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ"
dateFormatter.timeZone = NSTimeZone.defaultTimeZone()
return dateFormatter.stringFromDate(date)
}
}
我收到了这一行的错误:
let birthdateAsNSDate = Date.parseFacebookBirthdate(birthdateString)
答案 0 :(得分:1)
在调用我的birthdateString
函数时,我只需要打开birthdateAsNSDate
和Date
。
非常简单的修复=]
let birthdateString = KeychainManager.getString(KeychainManager.StaticVars.kFbBirthdate)
let birthdateAsNSDate = Date.parseFacebookBirthdate(birthdateString!)
let birthdateAsISOString = Date.getStringFromDate(birthdateAsNSDate!)