gpt4 book ai didi

ios - NSDateFormatter 在 iOS 7 上失败,适用于更高版本

转载 作者:行者123 更新时间:2023-11-28 08:54:41 26 4
gpt4 key购买 nike

我正在尝试使用以下代码解析格式为“ddMMyy-HH:mm:ss”的字符串

static func parseDate(var date: String?) -> NSDate?{
if(date == nil){
return nil
}
print("PARSING \(date)")
let formatter = NSDateFormatter()
formatter.dateFormat = "ddMMyy-HH:mm:ss"
let res = formatter.dateFromString(date!)
print("RESULT \(res)")
return res
}

res 的值最后是 nil,即使 date 不是 nil。然后我尝试使用

删除 : 和 - 符号
  date = date!.stringByReplacingOccurrencesOfString(":", withString: "")
date = date!.stringByReplacingOccurrencesOfString("-", withString: "")

然后更改我的格式以反射(reflect)这一点

formatter.dateFormat = "ddMMyyHHmmss"

现在控制台输出是

TIME IS 291015-12:10:12
PARSING Optional("291015121012")
RESULT nil

我也尝试添加行

formatter.locale = NSLocale.currentLocale()

奇怪的是,该应用程序在 iOS 9 上运行完美,我认为它在 iOS 8 上也能正常运行。

最佳答案

原来这个问题与 iOS 无关,这是因为我的 ios7 设备恰好在设备设置中处于 12 小时模式。这使格式化程序感到困惑。使用

    formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")

按照@WiliamKinaan 的建议修复了这个问题

关于ios - NSDateFormatter 在 iOS 7 上失败,适用于更高版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33413973/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com