Swift:无法将表达式的类型'$ T3'转换为'$ T4'类型

时间:2014-11-03 07:15:34

标签: ios xcode swift nsdate

我想要实现的是将生日字符串转换为以下格式:08/15/2014NSDate实例。然后将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)

1 个答案:

答案 0 :(得分:1)

在调用我的birthdateString函数时,我只需要打开birthdateAsNSDateDate

非常简单的修复=]

let birthdateString = KeychainManager.getString(KeychainManager.StaticVars.kFbBirthdate)
let birthdateAsNSDate = Date.parseFacebookBirthdate(birthdateString!)
let birthdateAsISOString = Date.getStringFromDate(birthdateAsNSDate!)